Alienor has asked for the wisdom of the Perl Monks concerning the following question:
Reverend Brothers and Sisters,
This humble non programmer seeks your wisdom in a puzzling (for me) matter.
Goal: The bit of code giving me a headache is intended to retrieve the text from all txt files in a directory. It will be used in another script that creates xml files including that text (I am proud to report that my xml script works just fine, processing one file at a time).
What I have so far:
use locale; use strict; use warnings; if ( $#ARGV != 0 ) { die "Fichier à ouvrir?\n"; } $/ = undef; open (FIC, $ARGV[0]) or die "Impossible d'ouvrir le fichier ", + "$ARGV[0] : ", "$!"; my $texte = <FIC>; print $texte, "\n"; close FIC;
use locale; use strict; use warnings; if ( $#ARGV != 0 ) { die "Dossier à ouvrir? Usage : perl openDir.pl /users/votr +eNom/Dossier\n"; } my $dossier = "$ARGV[0]"; opendir (DIR, $dossier) or die "Impossible d'ouvrir le dossier + ", "$dossier : ", "$!"; print "Fichiers en format texte :\n"; while (my $fichier = readdir (DIR)) { next if $fichier !~ ".\.txt"; print "$fichier\n"; } closedir (DIR);
My problem: Why when I put these two together it doesn't work? Is my logic flawed (Oh the shame!)? Here's the offending code:
use locale; use strict; use warnings; if ( $#ARGV != 0 ) { die "Dossier à traiter? Usage : perl openDirFile.pl /users/vot +reNom/Dossier\n"; } my $dossier = "$ARGV[0]"; opendir (DIR, $dossier) or die "Impossible d'ouvrir le dossier ", +"$dossier : ", "$!"; while (my $fichier = readdir (DIR)) { next if $fichier !~ ".\.txt"; $/ = undef; open (FIC, $fichier) or die "Impossible d'ouvrir le fichier ", + "$fichier : ", "$!"; my $texte = <FIC>; print $texte, "\n"; close FIC; } closedir (DIR);
When I run the program I get the error message: Impossible d'ouvrir le fichier MecaLatin.txt : No such file or directory at openDirFile.pl line 15. The aforementioned file is the first one in the directory.
I did try other ways to write the code and lost my sanity in www.perl.org, but I only managed to get more obscure error messages... (sigh!)
Other information that may (or probably will not) be useful: I use Perl 5.8.8 on Mac OS 10.5.8.
Eternal thanks in advance for any insights.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Trouble opening files from a directory: "no such file".
by toolic (Bishop) on Aug 09, 2011 at 19:52 UTC | |
by Alienor (Initiate) on Aug 09, 2011 at 20:05 UTC | |
|
Re: Trouble opening files from a directory: "no such file".
by Gulliver (Monk) on Aug 09, 2011 at 21:59 UTC | |
|
Re: Trouble opening files from a directory: "no such file".
by RichardK (Parson) on Aug 10, 2011 at 12:15 UTC | |
by Corion (Patriarch) on Aug 10, 2011 at 12:22 UTC |