Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重命名目录中的所有.txt文件,然后用perl打开该文件_Perl - Fatal编程技术网

重命名目录中的所有.txt文件,然后用perl打开该文件

重命名目录中的所有.txt文件,然后用perl打开该文件,perl,Perl,我需要一些文件操作方面的帮助,需要一些专家建议 看起来我在某个地方犯了一个愚蠢的错误,但我抓不住 我有一个目录,其中包含后缀为.txt的文件,例如file1.txt,file2.txt,file3.txt 我想在每个文件中添加一个修订字符串,比如说rev0,然后打开修改后的文件。例如rev0\u file1.txt,rev0\u file2.txt,rev0\u file3.txt 我可以附加rev0,但我的程序无法打开文件 下面是我代码的相关部分 my $dir = "path to my

我需要一些文件操作方面的帮助,需要一些专家建议

看起来我在某个地方犯了一个愚蠢的错误,但我抓不住

我有一个目录,其中包含后缀为
.txt
的文件,例如
file1.txt
file2.txt
file3.txt

我想在每个文件中添加一个修订字符串,比如说
rev0
,然后打开修改后的文件。例如
rev0\u file1.txt
rev0\u file2.txt
rev0\u file3.txt

我可以附加
rev0
,但我的程序无法打开文件

下面是我代码的相关部分

my $dir   = "path to my directory";
my @::tmp = ();

opendir(my $DIR, "$dir") or die "Can't open directory, $!";
@::list = readdir($DIR);
@::list2 = sort grep(/^.*\.txt$/, @::list);
foreach (@::list2) {
  my $new_file = "$::REV0" . "_" . "$_";
  print "new file is $new_file\n";
  push(@::tmp, "$new_file\n");
}
closedir($DIR);

foreach my $cur_file (<@::tmp>) {
  $cur_file = $_;
  print "Current file name is $cur_file\n"; # This debug print shows nothing
  open($fh, '<', "$cur_file") or die "Can't open the file\n"; # Fails to open file;
}
my$dir=“我的目录路径”;
我的@::tmp=();
opendir(my$DIR,“$DIR”)或die“无法打开目录,$!”;
@::list=readdir($DIR);
@::list2=排序grep(/^.*\.txt$/,@::list);
foreach(@::list2){
我的$new\u文件=“$::REV0”。“$”;
打印“新文件为$new\u文件\n”;
推送(@::tmp,“$new_file\n”);
}
closedir($DIR);
foreach我的$cur\u文件(){
$cur\u文件=$\u;
打印“当前文件名为$cur_file\n”;#此调试打印不显示任何内容
打开($fh,您的问题在这里:

foreach my $cur_file(<@::tmp>) {
    $cur_file = $_;
foreach my$cur_文件(){
$cur\u文件=$\u;

您正在使用循环变量
$cur\u file
,但是您用
$\u
覆盖了它,这在这个循环中根本没有使用。要解决这个问题,只需删除第二行。

您试图存储的是
@::tmp
中文件的更新名称。该文件没有被重命名,因此代码
没有什么奇怪的>die
d,因为它找不到重命名的文件

因为它只是重命名,请考虑下面的代码:

use strict;
use warnings;
use File::Copy 'move';

for my $file ( glob( "file*.txt" ) ) {

    move( $file, "rev0_$file" )
      or die "Unable to rename '$file': $!";
}

从命令行/终端,考虑<代码>重命名< /代码>实用程序(如果可用):

$ rename file rev0_file file*.txt
始终包括在每个脚本的顶部。并且在任何时候进行文件或目录处理

没有理由在变量前面加上
,因此请简化代码,如下所示:

use strict;
use warnings;
use autodie;

use File::Copy;

my $dir = "path to my directory";

chdir($dir); # Make easier by removing the need to prefix path information

foreach my $file (glob('*.txt')) {
    my $newfile = 'rev0_'.$file;
    copy($file, $newfile) or die "Can't copy $file -> $newfile: $!";

    open my $fh, '<', $newfile;
    # File processing
}
使用严格;
使用警告;
使用自动模具;
使用文件::复制;
my$dir=“指向我的目录的路径”;
chdir($dir);#不需要在路径信息前面加前缀,从而简化了操作
foreach my$文件(glob('*.txt')){
my$newfile='rev0.'$file;
复制($file,$newfile)或死亡“无法复制$file->$newfile:$!”;

打开我的$fh,你最大的问题是,你在循环中使用了
$cur\u file
作为文件名,但随后用
$\ucode>重新分配它,即使
$\ucode>在那一点上没有值。此外,正如Borodin指出的那样,
$:REV0
从未定义过

您可以使用
File::Copy
中的
move
命令移动文件,也可以使用
File::Find
查找要移动的文件:

use strict;
use warnings;
use feature qw(say);
use autodie;

use File::Copy;   # Provides the move command
use File::Find;   # Gives you a way to find the files you want

use constant {
    DIRECTORY  => '/path/to/directory',
    PREFIX     => 'rev0_',
};

my @files_to_rename;
find (
    sub {
       next unless /\.txt$/;   # Only interested in ".txt" files
       push @files_to_rename, $File::Find::name;
    }, DIRECTORY );

for my $file ( @files_to_rename ) {
    my $new_name = PREFIX . $file;
    move $file, $new_name;
    $file = $new_name;                              # Updates @files_to_rename with new name
    open my $file_fh, "<", $new_name;               # Open the file here?
    ...
    close $file_fh;

}

for my $file ( @files_to_rename ) {
    open my $file_fh, "<", $new_name;               # Or, open the file here?
    ...
    close $file_fh;
}
使用严格;
使用警告;
使用特征qw(例如);
使用自动模具;
use File::Copy;#提供移动命令
使用File::Find;#提供了一种查找所需文件的方法
使用常数{
目录=>'/path/to/DIRECTORY',
前缀=>“rev0\uu0”,
};
我的@files\u要重命名;
发现(
潜艇{
下一步除非/\.txt$/#只对“.txt”文件感兴趣
将@files\u推至\u重命名,$File::Find::name;
},目录);
对于我的$file(@files\u to\u rename){
我的$new_name=前缀。$file;
移动$file、$new_名称;
$file=$new_name;#将@files_更新为使用新名称重命名

打开我的$file\u fh,“你应该描述出什么地方出了问题,出现了什么错误。如果你没有收到任何错误,请添加
使用警告(你已经应该使用了)。另外,为什么要对变量使用
::
语法?这是试图绕过
使用strict
?因为如果是这样,这是一个非常糟糕的主意。我很惊讶
my@::tmp
不是语法错误。你确定
“$::REV0"
你想做什么?你在什么地方定义了
$::REV0
了吗?@Borodin看起来他在用
$::REV0
来定义变量,用
my
our
。他现在可以声称他使用
use strict;
而实际上不必使用它。@DavidW.:是的,这很不愉快,但是
>$::REV0
必须事先定义为
'REV0'
,它才能工作,我想知道他是否意识到这并不能回答
打开失败的原因。@Zaid:是的,因为代码试图打开由
$cur\u file
定义的文件,该文件是从
$\ucode>复制而来的,这是从某处留下的值在前面的代码中。@Borodin:无论TLP识别出什么错误,该文件从未被重命名。尝试打开重命名的文件可能会失败。
autodie
将处理
copy
中的所有错误,我能想到的是我的第一次代码迭代没有包含
或die
,但我在验证后添加了它g
autodie
中的文档没有包含
copy
。您让我仔细检查,以下内容没有抛出错误:
perl-Mautodie-MFile::copy=copy-e'copy qw(notreal.txt new_dest.txt)“
。我认为这实际上是首选功能,但必须三思而后行。
autodie
不会直接涵盖
copy
,因为它是由
File::copy
提供的函数,而不是内置运算符。我想你的意思是
notreal.txt
不存在?而你的
或die
版本确实存在es在相同的情况下会抛出错误?然后我需要找出原因。我希望模块的
sysopen
会因为文件不存在而失败,但根据平台状态,有多种方式通过代码:
所有函数在成功时返回1,在失败时返回0。如果遇到错误,将设置$ERD.
但是
autodie
的范围是词法的,所以应该