[英]how to append content of a string variable to a file using os.system
我有繩子
buff = "abc\ndef\n"
我需要使用os.system將此字符串的內容寫入文件
os.system('echo -e ' +buff+' > 123.txt')
這是我得到的錯誤
>>> os.system('echo -e ' +buff+' > 123.txt')
abc
sh: line 1: def: command not found
0
您應該使用引號將字符串封裝起來,因為它包含換行符:
os.system('echo -e "' + buff + '" > 123.txt')
要么
os.system("echo -e '" + buff + "' > 123.txt")
僅當您的文本不包含雙引號或雙引號時,此方法才有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.