I have string
buff = "abc\ndef\n"
I need to write the contents of this string to a file using os.system
os.system('echo -e ' +buff+' > 123.txt')
This is th error i am getting
>>> os.system('echo -e ' +buff+' > 123.txt')
abc
sh: line 1: def: command not found
0
You should encapsulate your string with quotes, because it contains newlines:
os.system('echo -e "' + buff + '" > 123.txt')
or
os.system("echo -e '" + buff + "' > 123.txt")
This only works if your text does not contain double quotes or quotes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.