如何使用将字符串变量的内容附加到文件操作系统

2024-04-29 06:41:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有绳子

buff = "abc\ndef\n"

我需要使用操作系统你知道吗

os.system('echo -e ' +buff+' > 123.txt')

这是我犯的错误

>>> os.system('echo -e ' +buff+' > 123.txt')
abc
sh: line 1: def: command not found

0个


Tags: echotxtosdefsh错误linenot
1条回答
网友
1楼 · 发布于 2024-04-29 06:41:20

您应该用引号封装字符串,因为它包含换行符:

os.system('echo -e "' + buff + '" > 123.txt')

或者

os.system("echo -e '" + buff + "' > 123.txt")

这只适用于文本不包含双引号或双引号的情况。你知道吗

相关问题 更多 >