我有一个脚本,它创建一些临时文件以供稍后处理,当不再需要它们时,我想删除它们。无法取消链接的文件始终是在
while
循环内创建的最后一个文件。感觉像是一个关闭问题,但我看不到它。 # Creates a temp file for each of the guys with every stock item from the products listed against their name.
open ENG_LIST, 'engineer_list.txt' or die "Can't open engineer_list.txt\n";
mkdir './Temp';
while ($line = <ENG_LIST>) {
chomp $line;
@products = split(/\|/, $line);
$engineer = shift @products;
push @engineer_list, $engineer;
$fh = "./Temp/$engineer.txt";
foreach $product (@products) {
open $output, ">>:raw", "./Temp/$engineer.txt" or die "Unable to open ./Temp/$engineer.txt for appending: $!\n";
open $input, "<:raw", "./Products/$product.txt" or die "Unable to open ./Products/$product.txt for reading: $!\n";
print {$output} <$input>;
}
close ($fh);
undef @products;
}
close (ENG_LIST);
然后是一些我很满意的处理。然后是清理程序:
# Cleaning up all the temp files. Need to fix this because it doesn't work!
@del_files = glob ('./Temp/*.txt');
foreach $del_file (@del_files) {
chomp $del_file;
close ($del_file);
unlink "$del_file";
}
rmtree ('./Temp');
我尝试过使用 chmod,但我也一无所获。错误总是存在
cannot unlink file - Permission denied
而不是
close ($fh);
您需要 close ($input);
和 close ($output);
。您正在尝试删除/unlink()
正在使用的文件。