perl判断文件和目录是否为空的代码,感兴趣的朋友可以参考下。
1,判断文件是否为空
使用 Tie::File 模块:
#!/usr/bin/perl -w
use strict;
use Tie::File;
my $filename = '/opt/test/aa';
tie my @array, 'Tie::File', $filename or die "$!";
if ( @array ) {
print "$filename is have file. n";
}
else {
print "$filename is empty. n";
}
2,判断目录是否为空
判断目录为空的方法,无非也是查看目录下是否有文件,注意也可以查看是否有隐藏文件,带#那行代码就可以。
#!/usr/bin/perl -w
use strict;
my $dirname = "/opt/null";
my @dir_files = <$dirname/*>;
#my @dir_files = <$dirname/* $dirname/.*>;
if ( @dir_files ) {
print "$dirname is have file. n";
foreach my $file (@dir_files) {
print "$filen";
}
}
else {
print "$dirname is empty. n";
}