将Shell输出写入文本文件

时间:2018-10-14 17:28:17

标签: python shell

如何将shell输出写入python中的文本文件?我想将shell输出另存为日志到文本文件中,而我目前正在使用spyder。

1 个答案:

答案 0 :(得分:1)

而不是仅使用print语句或print(statement),请使用:

a)一些日志记录模块。例如,看看logging模块。

b)打开输出文件,并将所有内容写入该文件。因此,在您的脚本中:

fout=open('log.txt','w')
# Here are your script things....
fout.write(statement+'\n')
# More script things ...
fout.close()

c)使用以下示例:How to save the output of an IPython console to a file in Spyder?

相关问题