[Tutor] Save output to log file

Alan Gauld alan.gauld at yahoo.co.uk
Mon Aug 17 03:58:56 EDT 2020


On 17/08/2020 05:36, Phaik Huen Tan wrote:
> I am supposed to create a network monitoring tool using python and send the
> output to log file. I have been searching online and don't have any luck
> finding how to send the result to log file. Can you please give me example
> on how to send the log from the output?


A log file is just a file. So if you can write to any file
you can write to a log file. The simplest way to  to that is just
to use print statements then, when you run the code use
output redirection to your log file

$ python myprog.py > mylogfile.txt

But assuming you need something more sophisticated than that
you can use the logging module that comes with Python.
Check the documentation for many examples.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list