write to file
Sharan Basappa
sharan.basappa at gmail.com
Sat May 5 11:45:39 EDT 2018
On Saturday, 5 May 2018 19:00:12 UTC+5:30, Steven D'Aprano wrote:
> 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
Steve,
Thanks a lot. I have actually tried print with file handle as a parameter (the last option). I see that the file is created but nothing is logged. So, I assumed, I am doing something incorrect.
Anyway, I will try this again and let you know how it goes.
More information about the Python-list
mailing list