perl文件大小计算不起作用 [英] perl file size calculation not working
问题描述
我正在尝试编写一个简单的perl脚本,该脚本将遍历目录中的常规文件并计算放在一起的所有文件的总大小.但是,我无法获取文件的实际大小,也无法弄清原因.这是代码的相关部分.我输入了打印语句以进行调试:
I am trying to write a simple perl script that will iterate through the regular files in a directory and calculate the total size of all the files put together. However, I am not able to get the actual size of the file, and I can't figure out why. Here is the relevant portion of the code. I put in print statements for debugging:
$totalsize = 0;
while ($_ = readdir (DH)) {
print "current file is: $_\t";
$cursize = -s $_;
print "size is: $cursize\n";
$totalsize += $cursize;
}
这是我得到的输出:
current file is: test.pl size is:
current file is: prob12.pl size is:
current file is: prob13.pl size is:
current file is: prob14.pl size is:
current file is: prob15.pl size is:
因此文件大小保持为空白.我尝试使用$cursize = $_
代替,但是唯一的效果是检索当前目录和父目录的文件大小,每个文件的大小为4096字节.它仍然没有获得常规文件的任何实际文件大小.
So the file size remains blank. I tried using $cursize = $_
instead but the only effect of that was to retrieve the file sizes for the current and parent directories as 4096 bytes each; it still didn't get any of the actual file sizes for the regular files.
我在网上浏览了几本关于perl的书,看来perl无法获得文件大小,因为脚本无法读取文件.我通过输入if语句进行了测试:
I have looked online and through a couple of books I have on perl, and it seems that perl isn't able to get the file sizes because the script can't read the files. I tested this by putting in an if statement:
print "Cannot read file $_\n" if (! -r _);
对于每个文件,我肯定会收到错误消息,指出无法读取该文件.我不明白为什么会这样.包含相关文件的目录是主目录的子目录,我从主目录中的另一个子目录中以自己的身份运行脚本.我具有所有相关文件的读取权限.我尝试将文件的模式更改为755(从以前的711),但是仍然获得每个文件的Cannot read file
输出.
Sure enough for each file I got the error saying that the file could not be read. I do not understand why this is happening. The directory that has the files in question is a subdirectory of my home directory, and I am running the script as myself from another subdirectory in my home directory. I have read permissions to all the relevant files. I tried changing the mode on the files to 755 (from the previous 711), but I still got the Cannot read file
output for each file.
我不知道发生了什么.我可能对运行perl脚本时权限的工作方式感到困惑,或者对使用-s _
的正确方法感到困惑.感谢您的指导.谢谢!
I do not understand what's going on. Either I am mixed up about how permissions work when running a perl script, or I am mixed up about the proper way to use -s _
. I appreciate your guidance. Thanks!
推荐答案
如果不是您的错字-s _
而不是正确的-s $_
,请记住readdir
返回相对于您目录的文件名已使用opendir
打开.正确的方法应该是
If it isn't just your typo -s _
instead of the correct -s $_
then please remember that readdir
returns file names relative to the directory you've opened with opendir
. The proper way would be something like
my $base_dir = '/path/to/somewhere';
opendir DH, $base_dir or die;
while ($_ = readdir DH) {
print "size of $_: " . (-s "$base_dir/$_") . "\n";
}
closedir DH;
您还可以查看核心模块 IO :: Dir 提供了一种tie
方式,可以更简单地访问文件名和属性.
You could also take a look at the core module IO::Dir which offers a tie
way of accessing both the file names and the attributes in a simpler manner.
这篇关于perl文件大小计算不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!