我想在我的桌面上打开一个现有文件并写入它,由于某些原因我不能在 ubuntu 中这样做。可能我写的路径不准确?
没有模块等是否可行
open(WF,'>','/home/user/Desktop/write1.txt';
$text = "I am writing to this file";
print WF $text;
close(WF);
print "Done!\n";
最佳答案
您必须以追加 (>>>
) 模式打开文件才能写入同一文件。
(使用现代方式读取文件,使用词法文件句柄:)
下面是代码片段(在 Ubuntu 20.04.1
和 Perl v5.30.0
中测试):
#!/usr/bin/perl
use strict;
use warnings;
my $filename = '/home/vkk/Scripts/outfile.txt';
open(my $fh, '>>', $filename) or die "Could not open file '$filename' $!";
print $fh "Write this line to file\n";
close $fh;
print "done\n";
有关更多信息,请参阅这些链接 - open或 appending-to-files由嘉宝。
关于perl - 如何写入 Perl 中的现有文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64602860/