得到错误"没有这样的文件或目录"在perl

时间:2017-03-27 07:40:36

标签: perl

这是我的代码,我用子例程传递文件。从子程序我无法打开文件。它正在抛出一个错误

  

"无法打开   inputFiles / Fundamental.FinancialLineItem.FinancialLineItem.SelfSourcedPublic.SHE.1.2017-01-11-2259.Full.txt   :在Practice_Dubugg.pl第40行没有这样的文件或目录。"

    use strict;
use warnings;
use Getopt::Std;
use FileHandle;

my %opts;
my $optstr = "i:o:";
getopts("$optstr", \%opts);
if($opts{i} eq '' || $opts{o} eq '' )
{
        print "usage: perl TextCompare_Fund.pl <-i INPUTFILE> <-o MAPREDUCE OUTPUTFILE>\n";
        die 1;
}
my $inputFilesPath=$opts{i};
my $outputFilesPath=$opts{o};
my @ifiles=`ls $inputFilesPath`;
my @ofiles=`ls $outputFilesPath`;
foreach my $ifile (@ifiles)
{
    my $ifile_substr=substr("$ifile",0,-25);
    foreach my $ofile (@ofiles)
    {
        my $ofile_substr=substr("$ofile",0,-21);
        my $result=$ifile_substr cmp $ofile_substr;
        if($result eq 0)
        {
            print "$result\n";
            #print "$ifile\n";
            compare($ifile,$ofile)
        }
    }
}
sub compare
{
    my $afile="$_[0]";
    my $bfile="$_[1]";
    my $path1="$inputFilesPath/$afile";
    my $path2="$outputFilesPath/$bfile";
    #open FILE, "<", $path1 or die "$!:$path1";
    open my $infile, "<", $path1 or die "Couldn't open $path1: $!";
    my %a_lines;
    my %b_lines;
    my $count1=0;
    while (my $line = <$infile>) 
    {
        chomp $line;
        $a_lines{$line} = undef;
        $count1=$count1+1;
    }
    print"File1 records count : $count1\n";
    close $infile;
    my $file=substr("$afile",0,-25);
    my $OUTPUT = "/hadoop/user/m6034690/Kishore/$file.comparision_result";
    open my $outfile, "<", $path2 or die "Couldn't open $path2: $!";
    open (OUTPUT, ">$OUTPUT") or die "Cannot open $OUTPUT \n";

    my $count=0;
    my $count2=0;
    while (my $line = <$outfile>) 
    {
        chomp $line;
        $b_lines{$line} = undef;
        $count2=$count2+1;
        next if exists $a_lines{$line};
        $count=$count+1;
        print OUTPUT "$line \t===> The Line which is selected from file2/arg2 is mismatching/not available in file1\n";
    }   
    print "File2 records count : $count2\n";
    print "Total mismatching/unavailable records in file1 : $count\n";
    close $outfile;
    close OUTPUT;
}

1 个答案:

答案 0 :(得分:0)

尝试在下面的评论之后添加以下行。

my $afile="$_[0]";
my $bfile="$_[1]";
my $path1="$inputFilesPath/$afile";
my $path2="$outputFilesPath/$bfile";

#comment, add the below chomps right under the above portion.
chomp $path1;
chomp $path2;

我的测试正常,因为路径现已正确格式化。

我的结果:

File1 records count : 1
File2 records count : 1
Total mismatching/unavailable records in file1 : 1

花了一段时间来解决这个问题,但有点令人困惑,因为脚本说用法是-i INPUTFILE-o OUTPUTFILE

这告诉我应该添加文件路径而不是文件夹的路径,但无论如何,问题都应该解决。

编辑: 一个更好的选择,我们应该在ls发生的地方添加chomp。

chomp( my @ifiles=`ls $inputFilesPath` );
chomp( my @ofiles=`ls $outputFilesPath` );