gpt4 book ai didi

perl - 无法在 Perl 中打开子目录中的文件

转载 作者:行者123 更新时间:2023-12-02 01:08:58 29 4
gpt4 key购买 nike

我真的很难过。我是 Perl 的新手,我完全无法尝试遍历所有子目录,执行操作。它在根目录(我的脚本所在的位置)中工作正常,但是一旦到达子目录,它就会爆炸。我试过在 "或 ' 或 q{} 中围绕文件名无济于事。我在 StackOverflow 上搜索了这个问题,人们说它应该可以正常工作,打开处理子目录并将接受正斜杠。一旦进入目录并尝试打开“./Horse/Models.meta”,它就会在第一次打开(读取文件)时失败。这是在它处理了与我的脚本相同级别的 20 个文件之后。有什么想法吗?

use 5.010;
use strict;
use warnings;
use File::Find;

find(sub {
if (-f and /\.meta$/) {
print "--> " . $File::Find::name . "\n";
open my $in, '<', $File::Find::name or die "Can't read old file: $!";
open my $out, '>', "$File::Find::name.new" or die "Can't write new file: $!";

while( <$in> )
{
if(index($_," assetBundleName:")!=-1) {
print $out " assetBundleName: mobs\n";
} else {
print $out $_;
}
}
close $out;
print("mv -f \"" . $File::Find::name . ".new\" \"" . $File::Find::name . "\"\n");

system("mv -f \"" . $File::Find::name . ".new\" \"" . $File::Find::name . "\"");

}
}, '.');

最佳答案

就用

rename "$File::Find::name.new", $File::Find::name or die $!;

参见 rename .

如果你想使用相对路径(即$File::Find::name)而不只是文件名(即$_),你需要指定

find({ wanted   => sub { ... },
no_chdir => 1,
}, '.');

关于perl - 无法在 Perl 中打开子目录中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46083908/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com