0

How can I write the shell output in to a text file in python? I want to save the shell output as a log in a text file and I am currently using spyder.

petezurich
  • 9,280
  • 9
  • 43
  • 57
Lakwin Chandula
  • 159
  • 1
  • 3
  • 15

1 Answers1

2

Instead of just using print statement or print(statement), use:

a) some logging modules. Have a look at logging module for example.

b) an opened file for the output and write everything to that file. So in your script:

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

c) use an example from this post: How to save the output of an IPython console to a file in Spyder?

msi_gerva
  • 2,021
  • 3
  • 22
  • 28