Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: cant open files from a directory

by vinoth.ree (Parson)
on Jan 31, 2014 at 07:20 UTC ( #1072797=note: print w/ replies, xml ) Need Help??


in reply to cant open files from a directory

parv++

readdir(D) gives only the file names, if you included the $! in the file open function it could have given you the "oops No such file or directory" error message, in the file open function give the absolute path to open the file.

Here is the code I checked.

my $handle=$ARGV[0]; opendir(D,$handle) or die "cannot open $!"; my @files=readdir(D); print "@files\n"; foreach my $file(@files) { open (MYFILE,"$handle/$file") or die "oops $!"; while(<MYFILE>) { print $_; } } closedir(D);


All is well


Comment on Re: cant open files from a directory
Select or Download Code
Re^2: cant open files from a directory
by x-lours (Acolyte) on Jan 31, 2014 at 15:15 UTC

    readdir gives not only the file names but also the directory names present inside your $handle.

    open (IN, "$handle/$file") on a directory will give you an error of course ;-)

    you will have to test $file for being a file before opening it.

      A directory is also a file.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1072797]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (13)
As of 2014-08-27 18:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (248 votes), past polls