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;

In reply to Failed to open file for write by wrkrbeee

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.