打开/操作目录中所有文件的最有效方法是什么? [英] What is the most efficient way to open/act upon all of the files in a directory?

查看:37
本文介绍了打开/操作目录中所有文件的最有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对目录的所有文件执行我的脚本(搜索).以下是有效的方法.我只是问哪个最好.(我需要表单的文件名:parsedchpt31_4.txt)

I need to perform my script (a search) on all the files of a directory. Here are the methods which work. I am just asking which is best. (I need file names of form: parsedchpt31_4.txt)

全局:

my $parse_corpus; #(for all options)
##glob (only if all files in same directory as script?):
my @files = glob("parsed"."*.txt");
foreach my $file (@files) {
    open($parse_corpus, '<', "$file") or die $!;
     ... all my code...
}

Readdir with while 和条件:

Readdir with while and conditions:

##readdir:
my $dir = '.';
opendir(DIR, $dir) or die $!;

while (my $file = readdir(DIR)) {
    next unless (-f "$dir/$file"); ##Ensure it's a file
    next unless ($file =~ m/^parsed.*\.txt/); ##Ensure it's a parsed file
    open($parse_corpus, '<', "$file") or die "Couldn't open directory $!";
     ... all my code...
}

使用 foreach 和 grep 读取目录:

Readdir with foreach and grep:

##readdir+grep:
my $dir = '.';
    opendir(DIR, $dir) or die $!;    
foreach my $file (grep {/^parsed.*\.txt/} readdir (DIR)) {
    next unless (-f "$dir/$file"); ##Ensure it's a file
    open($parse_corpus, '<', "$file") or die "Couldn't open directory $!";
    ... all my code...
}

文件::查找:

##File::Find
my $dir = "."; ##current directory: could be (include quotes): '/Users/jon/Desktop/...'
my @files;
find(\&open_file, $dir); ##built in function
sub open_file {
    push @files, $File::Find::name if(/^parsed.*\.txt/);
}
foreach my $file (@files) {
    open($parse_corpus, '<', "$file") or die $!;
     ...all my code...
} 

还有别的办法吗?将我的整个脚本包含在循环中好吗?我不使用 closedir 可以吗?我正在将其传递给其他人,我不确定他们的文件会在哪里(可能无法使用 glob)

Is there another way? Is it good to enclose my entire script in the loops? Is it okay I don't use closedir? I'm passing this off to others, I'm not sure where their files will be (may not be able to use glob)

非常感谢,希望这是问这个问题的正确地方.

Thanks a lot, hopefully this is the right place to ask this.

推荐答案

最佳或最有效的方法取决于您的目的和更大的上下文.你的意思是在原始速度、代码简单性还是其他方面最好?我怀疑内存考虑是否会推动这种选择.目录中有多少个文件?

The best or most efficient approach depends on your purposes and the larger context. Do you mean best in terms of raw speed, simplicity of the code, or something else? I'm skeptical that memory considerations should drive this choice. How many files are in the directory?

纯粹出于实用性考虑,glob 方法非常有效.在诉诸任何更复杂的事情之前,我会先问一下是否有问题.

For sheer practicality, the glob approach works fairly well. Before resorting to anything more involved, I'd ask whether there is a problem.

如果您能够使用其他模块,另一种方法是让其他人担心肮脏的细节:

If you're able to use other modules, another approach is to let someone else worry about the grubby details:

use File::Util qw();
my $fu = File::Util->new;
my @files = $fu->list_dir($dir, qw(--with-paths --files-only));

请注意,File::Find 执行递归搜索到所有子目录.很多时候你不想要或不需要它.

Note that File::Find performs a recursive search descending into all subdirectories. Many times you don't want or need that.

我还要补充一点,我不喜欢你的两个 readdir 示例,因为它们混合了不同的功能:(1) 获取文件名,以及 (2) 处理单个文件.我会把这些工作分开.

I would also add that I dislike your two readdir examples because they comingle different pieces of functionality: (1) getting file names, and (2) processing individual files. I would keep those jobs separate.

my $dir = '.';
opendir(my $dh, $dir) or die $!; # Use a lexical directory handle.
my @files = 
    grep { -f }
    map  { "$dir/$_" }
    grep { /^parsed.*\.txt$/ }
    readdir($dh);

for my $file (@files){
    ...
}

这篇关于打开/操作目录中所有文件的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆