我正在使用将目录名称作为用户输入并在其中搜索文件的perl脚本。搜索文件后,它读取文件的内容。如果文件内容包含一个单词“板球”,然后使用取消链接功能,我应该能够删除该文件。但是使用unlink
之后,包含单词“cricket”的文件在执行代码后仍然存在于目录中。请帮忙。我的代码是:perl无法使用Unlink删除文件
use strict;
use warnings;
use File::Basename;
print "enter a directory name\n";
my $dir = <>;
print "you have entered $dir \n";
chomp($dir);
opendir DIR, $dir or die "cannot open directory $!";
while (my $file = readdir(DIR)) {
next if ($file =~ m/^\./);
my $filepath = "${dir}${file}";
print "$filepath\n";
print " $file \n";
open(my $fh, '<', $filepath) or die "unable to open the $file $!";
my $count = 0;
while (my $row = <$fh>) {
chomp $row;
if ($row =~ /cricket/) {
$count++;
}
}
print "$count";
if ($count == 0) {
chomp($filepath);
unlink $filepath;
print " $filepath deleted";
}
}
您是否有权写入该文件? – McKracken 2014-11-09 07:29:47
看起来你正试图“解开”一个打开的文件。 – 2014-11-09 07:30:45