write to file
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Sat May 5 09:27:36 EDT 2018
On Sat, 05 May 2018 04:25:50 -0700, Sharan Basappa wrote:
> In my program, I have print statements for debugging. However, these are
> cluttering the display. So, I am trying to save these to a file but
> somehow I cant seem to get it correct.
There are lots of way to solve this problem.
The best way is to use the logging module. Here are a couple of
introductions to it:
https://docs.python.org/3/howto/logging.html#logging-basic-tutorial
https://pymotw.com/3/logging/index.html
and there are many more:
https://duckduckgo.com/?q=python+logging+tutorial
but we can start with something much simpler, just using print.
You have already run:
fh = open("ML_PY_2.log","w+")
at the start of your program, now call print:
print("target_names:",target_names, file=fh)
Finally, at the end of your program, call:
fh.close()
--
Steve
More information about the Python-list
mailing list