简体   繁体   中英

how to append content of a string variable to a file using os.system

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM