在perl中从文件夹中读取多个文件
我对perl非常陌生,需要一些帮助,基本上我想要的是一个从文件夹中读取所有.txt文件的程序,执行脚本并将输出以新名称放入新文件夹。当我一次处理一个文件,并指定该文件的名称时,一切都正常。。但我无法让它处理文件夹中的所有文件。我已经走了这么远了在perl中从文件夹中读取多个文件,perl,Perl,我对perl非常陌生,需要一些帮助,基本上我想要的是一个从文件夹中读取所有.txt文件的程序,执行脚本并将输出以新名称放入新文件夹。当我一次处理一个文件,并指定该文件的名称时,一切都正常。。但我无法让它处理文件夹中的所有文件。我已经走了这么远了 #!/usr/bin/perl use warnings; use strict; use Path::Class; use autodie; use File::Find; my @now = localtime(); my $ti
#!/usr/bin/perl
use warnings;
use strict;
use Path::Class;
use autodie;
use File::Find;
my @now = localtime();
my $timeStamp = sprintf(
"%04d%02d%02d-%02d:%02d:%02d",
$now[5] + 1900,
$now[4] + 1,
$now[3], $now[2], $now[1], $now[0]); #A function that translates time
my %wordcount;
my $dir = "/home/smenk/.filfolder";
opendir(DIR, $dir) || die "Kan inte öppna $dir: $!";
my @files = grep { /txt/ } readdir(DIR);
closedir DIR;
my $new_dir = dir("/home/smenk/.result"); # Reads in the folder for save
my $new_file = $new_dir->file("$timeStamp.log"); # Reads in the new file timestamp variable
open my $fh, '<', $dir or die "Kunde inte öppna '$dir' $!";
open my $fhn, '>', $new_file or die "test '$new_file'";
foreach my $file (@files) {
open(FH, "/home/smenk/.filfolder/$file") || die "Unable to open $file - $!\n";
while (<FH>) {
}
close(FH);
}
while (my $line = <$fh>) {
foreach my $str (split /\s+/, $line) {
$wordcount{$str}++;
}
}
my @listing = (sort { $wordcount{$b} <=> $wordcount{$a} } keys %wordcount)[0 .. 9];
foreach my $str (@listing) {
my $output = $wordcount{$str} . " $str\n";
print $fhn $output;
}
#/usr/bin/perl
使用警告;
严格使用;
使用Path::类;
使用自动模具;
使用File::Find;
my@now=localtime();
我的$timeStamp=sprintf(
%04d%02d%02d-%02d:%02d:%02d”,
$now[5]+1900,
$now[4]+1,
$now[3]、$now[2]、$now[1]、$now[0])#转换时间的函数
我的%字数;
my$dir=“/home/smink/.filfolder”;
opendir(DIR,$DIR)| die“Kan inteöppna$DIR:$!”;
my@files=grep{/txt/}readdir(DIR);
closedir;
我的$new_dir=dir(“/home/smink/.result”);#在文件夹中读取以进行保存
my$new_file=$new_dir->file($timeStamp.log);#读入新文件timestamp变量
打开我的$fh、、$new_文件或死“测试'$new_文件'”;
foreach my$文件(@files){
打开(FH,“/home/smink/.filfolder/$file”)| | die“无法打开$file-$!\n”;
而(){
}
关闭(FH);
}
while(我的$line=){
foreach my$str(拆分/\s+/,$line){
$wordcount{$str}++;
}
}
my@listing=(排序{$wordcount{$b}$wordcount{$a}}关键字%wordcount)[0..9];
foreach my$str(@listing){
my$output=$wordcount{$str}.“$str\n”;
打印$fhn$输出;
}
以下是阅读部分使用的最简单框架(另请参见和:
!/usr/bin/perl
使用警告;
严格使用;
使用Path::类;
我的$src=dir(“/home/smink/.filfolder”);
my@txt_files=grep/[.]txt\z/x,$src->children;
对于我的$txt_文件(@txt_文件){
我的$in=$txt\U文件->openr;
while(我的$line=){
打印“输出:$line”;
}
}
您还可以使用另一个强大的模块,用于目录/文件操作和日期/时间功能,如:
#!/usr/bin/env perl
use strict;
use warnings;
use Path::Tiny;
use Time::Piece;
my @txtfiles = path("/home/smenk/.filfolder")->children(qr/\.txt\z/);
my $outdir = path("home/smenk/.result");
$outdir->mkpath; #create the dir...
my $t = localtime;
my $outfile = $outdir->child($t->strftime("%Y%m%d-%H%M%S.txt"));
$outfile->touch;
my @outdata;
for my $infile (@txtfiles) {
my @lines = $infile->lines({chomp => 1});
#do something with lines and create the output @data
push @outdata, scalar @lines;
}
$outfile->append({truncate => 1}, map { "$_\n" } @outdata); #or spew;
也许您在这里输入了一个错误:
open(FH,“/home/smink/.filfolde/$file”)
.filfolder
应该是.filfolder
,不是吗?修复了这个问题,谢谢,仍然会得到空文件,因为你在这里混合了这么多不同的东西。如果你打算使用,例如,Path::Class::dir
,你不需要opendir
/readdir
。你永远不会给$fh
正在打开一个目录,就好像它是一个文件一样,它不能做任何有用的事情。它不会导致问题的唯一原因是因为您也从来没有使用过它。谢谢,我确实将它用作框架并实现了我自己的代码(经过一些清理)现在它完全按照我的要求工作!指向dir和file的死链接
#!/usr/bin/env perl
use strict;
use warnings;
use Path::Tiny;
use Time::Piece;
my @txtfiles = path("/home/smenk/.filfolder")->children(qr/\.txt\z/);
my $outdir = path("home/smenk/.result");
$outdir->mkpath; #create the dir...
my $t = localtime;
my $outfile = $outdir->child($t->strftime("%Y%m%d-%H%M%S.txt"));
$outfile->touch;
my @outdata;
for my $infile (@txtfiles) {
my @lines = $infile->lines({chomp => 1});
#do something with lines and create the output @data
push @outdata, scalar @lines;
}
$outfile->append({truncate => 1}, map { "$_\n" } @outdata); #or spew;