如何使用 Perl 列出目录中的所有文件?

发布于 2024-09-24 16:17:59 字数 264 浏览 0 评论 0原文

我通常使用类似的东西

my $dir="/path/to/dir";
opendir(DIR, $dir) or die "can't open $dir: $!";
my @files = readdir DIR;
closedir DIR;

,有时我使用 glob ,但无论如何,我总是需要添加一两行来过滤掉 ... 这很烦人。 您通常如何完成这项常见任务?

I usually use something like

my $dir="/path/to/dir";
opendir(DIR, $dir) or die "can't open $dir: $!";
my @files = readdir DIR;
closedir DIR;

or sometimes I use glob, but anyway, I always need to add a line or two to filter out . and .. which is quite annoying.
How do you usually go about this common task?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

桜花祭 2024-10-01 16:17:59
my @files = grep {!/^\./} readdir DIR;

这也将排除所有点文件,但这通常就是您想要的。

my @files = grep {!/^\./} readdir DIR;

This will exclude all the dotfiles as well, but that's usually What You Want.

春花秋月 2024-10-01 16:17:59

我经常使用 File::Slurp。好处包括: (1) 如果目录不存在则自动终止。 (2) 默认排除 ...。它的行为类似于 readdir ,因为它不返回完整路径。

use File::Slurp qw(read_dir);

my $dir = '/path/to/dir';
my @contents = read_dir($dir);

另一个有用的模块是 File::Util,它在读取目录时提供了许多选项。例如:

use File::Util;
my $dir = '/path/to/dir';
my $fu = File::Util->new;
my @contents = $fu->list_dir( $dir, '--with-paths', '--no-fsdots' );

I often use File::Slurp. Benefits include: (1) Dies automatically if the directory does not exist. (2) Excludes . and .. by default. It's behavior is like readdir in that it does not return the full paths.

use File::Slurp qw(read_dir);

my $dir = '/path/to/dir';
my @contents = read_dir($dir);

Another useful module is File::Util, which provides many options when reading a directory. For example:

use File::Util;
my $dir = '/path/to/dir';
my $fu = File::Util->new;
my @contents = $fu->list_dir( $dir, '--with-paths', '--no-fsdots' );
北风几吹夏 2024-10-01 16:17:59

我通常会使用 glob 方法:

for my $file (glob "$dir/*") {
    #do stuff with $file
}

除非目录中有很多文件,否则这种方法可以正常工作。在这些情况下,您必须在 while 循环中切换回 readdir (将 readdir 放入列表上下文中与 glob):

open my $dh, $dir
    or die "could not open $dir: $!";

while (my $file = readdir $dh) {
    next if $file =~ /^[.]/;
    #do stuff with $file
}

但通常,如果我正在读取目录中的一堆文件,我想以递归方式读取它们。在这些情况下,我使用 File::Find

use File::Find;

find sub {
    return if /^[.]/;
    #do stuff with $_ or $File::Find::name
}, $dir;

I will normally use the glob method:

for my $file (glob "$dir/*") {
    #do stuff with $file
}

This works fine unless the directory has lots of files in it. In those cases you have to switch back to readdir in a while loop (putting readdir in list context is just as bad as the glob):

open my $dh, $dir
    or die "could not open $dir: $!";

while (my $file = readdir $dh) {
    next if $file =~ /^[.]/;
    #do stuff with $file
}

Often though, if I am reading a bunch of files in a directory, I want to read them in a recursive manner. In those cases I use File::Find:

use File::Find;

find sub {
    return if /^[.]/;
    #do stuff with $_ or $File::Find::name
}, $dir;
浅唱ヾ落雨殇 2024-10-01 16:17:59

如果某些点文件很重要,

my @files = grep !/^\.\.?$/, readdir DIR;

则仅排除 ...

If some of the dotfiles are important,

my @files = grep !/^\.\.?$/, readdir DIR;

will only exclude . and ..

心病无药医 2024-10-01 16:17:59

当我只想要文件(而不是目录)时,我使用 grep-f 测试:

my @files = grep { -f } readdir $dir;

When I just want the files (as opposed to directories), I use grep with a -f test:

my @files = grep { -f } readdir $dir;
昵称有卵用 2024-10-01 16:17:59

感谢克里斯和以太的建议。我使用以下命令将所有文件(排除的目录)的列表从引用当前目录以外的目录的目录句柄读取到数组中。 grep 语句中不使用绝对路径时,数组总是缺少一个文件

use File::Slurp; 

print "\nWhich folder do you want to replace text? " ;
chomp (my $input = <>);
if ($input eq "") {
print "\nNo folder entered exiting program!!!\n";
exit 0;
} 

opendir(my $dh, $input) or die "\nUnable to access directory $input!!!\n"; 

my @dir = grep { -f "$input\\$_" } readdir $dh;

Thanks Chris and Ether for your recommendations. I used the following to read a listing of all files (excluded directories), from a directory handle referencing a directory other than my current directory, into an array. The array was always missing one file when not using the absolute path in the grep statement

use File::Slurp; 

print "\nWhich folder do you want to replace text? " ;
chomp (my $input = <>);
if ($input eq "") {
print "\nNo folder entered exiting program!!!\n";
exit 0;
} 

opendir(my $dh, $input) or die "\nUnable to access directory $input!!!\n"; 

my @dir = grep { -f "$input\\$_" } readdir $dh;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文