【问题标题】:Getting an error as "No such file or directory" in perl在 perl 中出现“没有这样的文件或目录”的错误
【发布时间】:2017-03-27 07:40:35
【问题描述】:

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

“无法打开 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;
}

【问题讨论】:

  • 看起来你有一个不适合的相对路径
  • 你运行的是什么操作系统,我可以看到它是基于 unix 的,但它是 Redhat、Suse 等吗?错误非常具体,那么您如何传递路径?
  • 我运行了脚本,它没有给我错误,因为我传递了正确的路径。显示你是如何运行脚本的?运行时发送完整的命令。
  • 我只在 unix 机器上运行这个脚本。即使我也传递了正确的路径,但文件仍然无法打开。
  • 子计数 { $path="$_[0]/$_[1]"; #$file="$_[0]"; #my $filepath = "$path/$file"; open FILE, ") { chomp $line; $a_lines{$line} = undef; $count1=$count1+1; } print "$count1" } $file="Fundamental.FinancialLineItem.FinancialLineItem.SelfSourcedPublic.SHE.1.2017-01-11-2259.Full.txt"; $path="/hadoop/user/m6034690/Kishore/inputFiles/"; count($path,$file) ==>我写了这个小脚本来验证路径。

标签: perl


【解决方案1】:

尝试在下面的注释后面添加以下行。

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` );

【讨论】:

  • 哇...伟大的格里。现在对我来说效果很好。非常感谢。我找不到那个问题。
  • 快乐的人。真的很简单。 ls 正在使用 \n 字符创建每个结果,而 chomp 正在删除它。
  • 嘿,格里,我还需要一个帮助。我正在比较两个非常大的文件,每个文件的大小为 40GB。在运行我的脚本时,它已被自动杀死。你能帮助如何提高我的脚本运行如此大文件的性能吗?
  • @nmkishore,我认为您应该考虑 sort 并同时读取两个文件并即时比较它们。
  • 现在上面的脚本能够比较大约 9GB 到 10GB 的文件。但我想与超过 50GB 的非常大的文件进行比较。即使是排序也可能需要更多时间来对这两个文件进行排序。有没有其他选择。请提出建议。
猜你喜欢
  • 1970-01-01
  • 2010-12-18
  • 2017-12-08
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-14
  • 2020-04-09
相关资源
最近更新 更多