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

File that exists won't open

by cormanaz (Chaplain)
on Mar 07, 2014 at 13:24 UTC ( #1077376=perlquestion: print w/replies, xml ) Need Help??
cormanaz has asked for the wisdom of the Perl Monks concerning the following question:

Good day bros. I almost feel stupid asking about this. I've been coding PERL for a long time, and this is a simple script, but I am getting a result I can't understand. Maybe it's too early in the morning and I've made one of those stupid mistakes you just can see for some reason. Anyway here is the code:
#!/usr/bin/perl use strict; my $targetdir = 'bmd/production'; my @coders = qw(fleischer); foreach my $c (@coders) { print "$c\n"; open(IN,"$c-assignments.dat") or die "Can't open input: $!\n"; my @coderfiles; while (<IN>) { chop; if ($_ =~ /\w/) { push(@coderfiles,$_); } } close IN; foreach my $f (@coderfiles) { print "$f\n"; my ($countrydir) = split(/\-/,$f); my $ofn = "$countrydir/$f"; print "$ofn\n"; my $nfn = "/usr/local/brat/data/$targetdir/$c/$countrydir/$f"; print "$nfn\n"; open(IN,$ofn.'.txt') or die "Can't open input: $!\n"; open(OUT,">$nfn".'.txt') or die "Can't open output: $!\n"; while (<IN>) { print OUT $_; } close IN; close OUT; open(OUT,">$nfn\.ann") or die "Can't open output: $!"; print OUT ''; close OUT; } }
There is only one person in the @coders array because I am testing. Here is what I get when I run this:
fleischer uk-07-m-01 uk/uk-07-m-01 /usr/local/brat/data/bmd/production/fleischer/uk/uk-07-m-01 Can't open input: No such file or directory
But the script is in the directory above uk and...
bash-3.2$ ls uk/uk-07-m-01.txt uk/uk-07-m-01.txt
It appears the .txt filetype is not getting appended to the filename. I have tried expressing the filename every way I can think of, for example "$ofn\.txt" which I normally do and have done a thousand times before, and I get the same result. Just for laughs, I commented out everything below the first open in the foreach loop, and added
print "$ofn\.txt\n"; print $ofn.'.txt'."\n"; print "$nfn\.txt\n"; print $nfn.'.txt'."\n";
And I get
fleischer uk-07-m-01 uk/uk-07-m-01 /usr/local/brat/data/bmd/production/fleischer/uk/uk-07-m-01 .txtk-07-m-01 .txtk-07-m-01 .txt/local/brat/data/bmd/production/fleischer/uk/uk-07-m-01 .txt/local/brat/data/bmd/production/fleischer/uk/uk-07-m-01

What am I doing wrong?

Replies are listed 'Best First'.
Re: File that exists won't open
by Corion (Pope) on Mar 07, 2014 at 13:29 UTC

    Your input files end in whitespace. Most likely, "Windows"-newlines, that is, \r\n. Your chop removes the last character, which would be the \n, but still leaves the \r at the end of the filename.

    Had you used warnings, Perl would've maybe told you about "failed open on filename containing newline".

    Just remove all whitespace from the end of your input filenames and retry.

    Also, I would adjust the error messages to be more informative by telling you what exactly failed:

    my $input_filename= "$ofn.txt"; open(IN,$input_filename) or die "Can't open input file '$input +_filename': $!\n"; my $output_filename= "$nfn.txt"; open(OUT,'>', $output_filename) or die "Can't open output file + '$output_filename': $!\n";
      That did it. If you would like to come by and give me a dope-slap, I live in Phoenix.

        That's why it is better to use Data::Dumper et. al. to print debug statements. :-)


Re: File that exists won't open
by marinersk (Priest) on Mar 07, 2014 at 13:43 UTC

    I am also curious why you use chop instead of chomp, which, if memory serves (and untested by me in recent memory), is more flexible and better suited to this particular action.

      What's not mentioned is that ALL last characters are problematic, not just line ending ones. The last letter of the file extension is always overly pesky.
Re: File that exists won't open
by ww (Archbishop) on Mar 07, 2014 at 13:58 UTC
    Just for giggles ...and because I've seen peculiar but inconsistent problems when including a hyphen (minus-sign) or appending one in a variable name under AS 5.10 ... 5.16, I tried some variants on your Line 007 (using "perl 5, version 16, subversion 3 (v5.16.3) built for MSWin32-x86-multi-thread" and escaping the double quotes to keep the Win32 shell happy, since my Linux box is otherwise occupied):
    C:\>perl -E "my $c; open(IN,"$c-assignments.dat") or die "Can't open i +nput: $!\n"; Can::t at -e line 1. C:\>perl -E "my $c; open(IN,\"$c-assignments.dat\") or die \"Can't ope +n input: $!\n\"" Can't open input: No such file or directory C:\>perl -E "my $c; say $c - assignments.dat;" 0dat C:\>perl -E "my $c; say $c-assignments.dat;" 0dat C:\>perl -E "my $c; say \"$c-assignments.dat\";" -assignments.dat

    The modified double-quotes shouldn't (TBOMK) change the way Perl interprets the code; they just keep the shell from diddling with the intent. But, as you can see, this for-giggles code suggests that yours won't do what you intended.

    Why strict is not squawking is another question: I can't explain why Perl should see "$c-assignments.dat" as a valid variable, when only "$c" has been declared but your report doesn't tell us that you say any such complaints.

    Bottom line: is what you posted an exact cut'n'paste of the code giving you problems?

    Come, let us reason together: Spirit of the Monastery

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1077376]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2017-12-14 19:44 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (408 votes). Check out past polls.