wrkrbeee has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks, I receive the following message when I execute the code below: Failed to open ex21.htm for write at line 27. Line 27 is my attempt to save the results to disk (i.e., open my $fh_out, '>', $write_dir.'/'.$filename or die "failed to open '$filename' for write";) It should be so simple, but I don't see it. Grateful for any help! Thank you!
#! /usr/bin/perl -w use strict; use warnings; use lib "c:/strawberry/perl/site/lib"; use open ':std', ':encoding(UTF-8)'; #Specify location of input files; my $files_dir = 'D:\research\audit fee models\filings\test'; #Specify location of output results; my $write_dir = 'D:\research\audit fee models\filings\filenames\filena +mes.txt'; #Open the directory containing the input files you will loop through; opendir (my $dir_handle, $files_dir); #Set up a loop for the sequentially reading the input files; while (my $filename = readdir($dir_handle)) { next unless -f $files_dir.'/'.$filename; print "Procesing $filename\n"; #Open the next file in the directory; open my $fh_in, '<', $files_dir.'/'.$filename or die "failed to open '$filename' for read"; #Open the output file for saving the results; open my $fh_out, '>', $write_dir.'/'.$filename or die "failed to open '$filename' for write"; #Initialize file counter; my $count=0; #Establish a loop to read the open file line by line; while (my $line = <$fh_in>) { #begin WHILE loop; print $line; print $fh_out "$line\n"; #write results to file; ++$count; #update line counter; } #end of while loop for current line; print "$count lines read from $filename\n;" } #end of current file loop;

Replies are listed 'Best First'.
Re: Failed to open file for write
by Laurent_R (Canon) on Apr 13, 2016 at 06:25 UTC
    Hi,

    you've found your problem and its solution by now, but just two debugging tips that may help you in the future.

    #Open the output file for saving the results; open my $fh_out, '>', $write_dir.'/'.$filename or die "failed to open '$filename' for write";
    Your error message could be improved by replacing the bare filename with the full name with path, and also by printing the $! special variable which will tells you the error reported by the operating system. So, something like this:
    my $full_filename = $write_dir.'/'.$filename; open my $fh_out, '>', $full_filename or die "failed to open $full_filename for write $!";
    would probably have led you to find the error by yourself.
Re: Failed to open file for write
by BrowserUk (Patriarch) on Apr 12, 2016 at 23:16 UTC

    Your backslashes are being swallowed by interpolation.

    If that means nothing to you; then try switching all the backslashes in your paths to forward slashes. Ie:

    'D:/research/audit fee models/filings/filenames/filenames.txt'

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Actually they aren't. Perl doesn't interpolate single quoted strings. The actual issue is that OP defined a file for output and is then trying to tack a filename onto the end of it and open it for writing, ie 'D:\research\audit fee models\filings\filenames\filenames.txt'.'/'.$filename

      No idea about Linux, but this doesn't work in a Windows world

      No argument here, but never had this problem with backslashes before. Changed to forward slashes, but now get the following message: "my" variable $files_dir masks earlier declaration in same scope at line 14. Line 14 is: opendir (my $dir_handle, $files_dir); What is this telling me? Sorry for the novice questions.

        Based on your OP code; I cannot see a problem that would cause that error; but you have modified your code and that error message is a typical catchall report that is manifest when perl is confused by a typo.

        The fastest solution would be to post the modified code that is producing the error.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Failed to open file for write
by jellisii2 (Hermit) on Apr 13, 2016 at 16:58 UTC