Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Unable to read files from a directory

by graff (Chancellor)
on May 14, 2009 at 03:43 UTC ( #763987=note: print w/replies, xml ) Need Help??


in reply to Unable to read files from a directory

First, add use strict;

Next, change the "readdir" line to be like this:

@files = grep { -f } readdir DIR;
That will assure that the @files array contains only things that can properly be opened as files (i.e. things that are not other directories).

Then, as mentioned above, make sure your "die" messages include the actual variable that contains the thing you were trying to open (directory name at line 2, file name at line 7). When you get a "permission denied" error on trying to open a file, check whether some other windows app happens to have that file open. (I gather that on Windows, when some process has a file opened for read/write access, other processes are not allowed to access that file.)

Once you know the specific name of the file that cannot be opened, you could check by other means to see whether some other program might have the same problem with that particular file.

Just a thought: since you are (presumably) looping over a number of files and just reporting some sort of result for each one, why not handle the open failures like this:

my $path = "C:/Perl/bin/Anti"; opendir( DIR, $path ) or die "opendir failed on $path: $!\n"; my @files = grep {-f "$path/$file"} readdir DIR; ### updated as per b +luto's comment below closedir DIR; for my $file ( @files ) { open( MYFILE, "<", "$path/$file" ) or do { warn "open failed on $path/$file: $! -- moving on...\n"; next; }; while (<MYFILE>) { ... } }
Finally, I'm puzzled by the apparent logic of the inner-most for loop. Looks like it shouldn't really be a loop, but in any case, it doesn't make sense.

I don't understand what you meant in your later reply when you said "I opened all the permissions". What does that mean, exactly?

Replies are listed 'Best First'.
Re^2: Unable to read files from a directory
by bluto (Curate) on May 14, 2009 at 15:07 UTC
    Next, change the "readdir" line to be like this:

    @files = grep { -f } readdir DIR;

    That will assure that the @files array contains only things that can properly be opened as files (i.e. things that are not other directories).

    This will only work when the current directory happens to be the same as $path. readdir returns bare file names, so in the general case a file should be turned into a full path before sending it to -f. I've been bitten by this more times than I can count (e.g. it works "fine" during testing, but then fails under normal use).

      keeps biting still...nothing seemed to work on Windows...ported it to Linux...works like a charm...what the heck??!!!
Re^2: Unable to read files from a directory
by antidote1316 (Initiate) on May 14, 2009 at 05:25 UTC
    done...again open fails! see this
    #!/usr/bin/perl @files = <C:/Perl/bin/Anti/*>; foreach $file (@files){ print "$file \n"; # found to be ok....prints all the files... open(MYFILE,"<","C:/Perl/bin/Anti/$file") or die "Failed to open file: + $file:$! "; while(<MYFILE>) { print "$_ \n \n \n"; } }
    it prints all the file names... I do not have any sub directories...but I used grep also... fails on file 1. gosh! hey...sorry for the earlier mistakes...I was lost in some world...the count is for some other purpose...was using a stupid method to see the counts of loops... and when I said opened, I meant all permissions... I am able to extract data from the individual files but when I run a loop for the folder, the files are not being opened...its acting weird! thanks for the assistance guys! much appreciated!
      Regarding this latest version of your code, this attempt is failing because now you are populating "@files" with full path strings (because you are using th file glob operator, instead of readdir), and then you are appending the path again inside the foreach loop.

      Using the file glob is a good idea, so now just fix the open statement:

      #!/usr/bin/perl use strict; my @files = <C:/Perl/bin/Anti/*>; for my $file (@files) { my count = 0; open(MYFILE,"<",$file) or do { warn "Failed to open file: $file:$! +\n"; next}; while(<MYFILE>) { $count++; } print "$file has $count lines\n"; }
      BTW, I like proper indenting... don't you?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2021-11-29 15:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?