gpt4 book ai didi

perl - 如何写入 Perl 中的现有文件?

转载 作者:行者123 更新时间:2023-12-05 09:37:00 27 4
gpt4 key购买 nike

我想在我的桌面上打开一个现有文件并写入它,由于某些原因我不能在 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.1Perl 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";

有关更多信息,请参阅这些链接 - openappending-to-files由嘉宝。

关于perl - 如何写入 Perl 中的现有文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64602860/

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