I have a few sub-folders in the main folder. There is a .txt file in each sub-folder. Firstly the code will create the "result" folder in the main folder. And it will search "atom" word in the .txt and print to the output file (in the result folder) as a column the columns including "Frequencies --" word. It will do this processes for each .txt file in sub-folders. The output file names can be the same to sub-folders. The code creates the "result" folder but it gives the following error. How can I fix it?
Error:Failed to open "./freq.txt" for writing: No such file or directory at ./analysis.pl line 27
#!/usr/bin/env perl use strict; use warnings; use File::Path qw/make_path/; use Cwd; my $dir = cwd(); opendir (my $dh, $dir) or die "Unable to open current directory! $!\n"; my @subdirs = grep { /^\.\.?\z/ } readdir($dh) or die "Unable to read directory! $!\n"; closedir $dh; my $result_path = "$dir/results"; make_path("$result_path"); for my $subdir ( sort @subdirs ) { chdir($dir); next unless -d $subdir; make_path("$result_path/$subdir"); my $search_text1 = qr/Frequencies --/; my $infile1="$subdir/freq.txt"; my $outfile1="$result_path/output.txt"; # Line 27 below open my $in_fh1, '<', $infile1 or die qq{Failed to open "$infile1" for writing: $!}; open my $out_fh1,'>', $outfile1 or die qq{Failed to open "$outfile1" for writing: $!}; while (<$in_fh1>) { next unless /$search_text1/; my @fields1 = split; print $out_fh1 join("\t", $fields1[1,2,3]), "\n"; } close($in_fh1); close($out_fh1); chdir(".."); } Input file:
Frequencies -- 23.5214 40.9023 56.7856 Red. masses -- 6.7793 1.0546 5.5674 Frc consts -- 0.0022 0.0010 0.0106 IR Inten -- 2.4504 0.2236 0.6152 Atom AN X Y Z X Y Z X Y Z 1 6 0.00 0.01 0.06 0.00 0.00 0.01 0.00 -0.01 0.11 2 6 0.00 0.00 0.09 0.00 0.00 0.01 0.00 0.00 0.10 3 7 0.00 0.01 0.19 0.00 0.00 0.03 0.00 0.00 0.02 Frequencies -- 91.1714 97.2522 123.2844 Red. masses -- 7.3071 9.6551 6.3036 Frc consts -- 0.0358 0.0538 0.0564 IR Inten -- 0.5639 11.9103 2.8105 Atom AN X Y Z X Y Z X Y Z 1 6 0.01 -0.14 -0.01 -0.01 0.04 0.04 0.00 0.00 -0.23 2 6 0.00 -0.12 0.02 0.00 0.04 0.02 0.01 0.01 0.17 3 7 0.00 -0.14 -0.05 0.00 0.04 -0.22 0.01 0.00 -0.15
.and..instead of the real subdirectories. You seem to be repeatedly asking very similar questions, and then ignoring the answers you get. Why are you not using the solution you accepted to your previous question?