Python Forum
Save console output to text file (like a log)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Save console output to text file (like a log)
#1
I have a python script that runs and it outputs things along the way. It's important that it outputs them in the command line, but at the end, I want to save whatever it output to a txt file, like a log. How would I do this? Thanks in advance!
Reply
#2
check logging module. you can specify both console and file handler(s)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Jun-02-2019, 04:57 PM)buran Wrote: check logging module. you can specify both console and file handler(s)

Thanks, this is helpful! If I'm reading this correctly, I have to both print things and send them to the log, is that right? So like this:

print('Data Ingestion Successful')
logging.info('Data Ingestion Successful')
There's no way for it to just capture everything that gets output? As part of the script, it runs a 3rd party program and half of the output is from that program - trying to capture everything, and not seeing a simple way to do that? Thanks again!
Reply
#4
(Jun-02-2019, 05:15 PM)koticphreak Wrote: As part of the script, it runs a 3rd party program and half of the output is from that program - trying to capture everything, and not seeing a simple way to do that?
how do you run this 3-rd party program?
you can capture the output when run it using subprocess module
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Jun-02-2019, 05:15 PM)koticphreak Wrote: this is helpful! If I'm reading this correctly, I have to both print things and send them to the log, is that right?
you can specify console handler and it will output to console. then if you specify also file handler/rotating file handler then you can have the output also in the log, e..
#set console handler
#set file handler
logging.info('Data Ingestion Successful') # this will go in both file and console
there are tutorials how to set the logging handlers
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(Jun-02-2019, 05:31 PM)buran Wrote:
(Jun-02-2019, 05:15 PM)koticphreak Wrote: As part of the script, it runs a 3rd party program and half of the output is from that program - trying to capture everything, and not seeing a simple way to do that?
how do you run this 3-rd party program?
you can capture the output when run it using subprocess module

Yes, that's exactly how I do it, using subprocess. Struggling to understand how to have it log it all. Having the logging module setup, but can only seem to pass things to the log using "logging.info/debug/warning", rather than having it just capture everything.
Reply
#7
https://stackoverflow.com/questions/4675...-in-python
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
An example using logging and catch subprocess output.
Using logzero simplify logging,as don't have to write all that boilerplate logging code.
import subprocess
import logzero
from logzero import logger

# 3 rotations, each with a maximum filesize of 1MB:
logzero.logfile("logfile.log", maxBytes=1e6, backupCount=3, disableStderrLogger=True)

out = subprocess.run(['ping', 'google.com'], capture_output=True)
output = out.stdout.decode()
logger.info(output)
So now output only goes to logfile,disableStderrLogger=True False if want output also in console.
Output:
[I 190602 20:45:39 log_zero:10] Pinging google.com [2a00:1450:400f:809::200e] with 32 bytes of data: Request timed out. Request timed out. Reply from 2a00:1450:400f:809::200e: time=94ms Reply from 2a00:1450:400f:809::200e: time=84ms Ping statistics for 2a00:1450:400f:809::200e: Packets: Sent = 4, Received = 2, Lost = 2 (50% loss), Approximate round trip times in milli-seconds: Minimum = 84ms, Maximum = 94ms, Average = 89ms
Reply
#9
(Jun-02-2019, 06:48 PM)snippsat Wrote: Using logzero simplify logging,as don't have to write all that boilerplate logging code.

Thanks for the help! Getting this error:

Error:
Traceback (most recent call last): File "C:\script.py", line 83, in <module> output = out.stdout.decode() UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa9 in position 44: invalid start byte
Reply
#10
Take away .decode(),will save in bytes this may be okay as you can work with bytes to.
Your output can have different encoding than utf-8.
Try .decode('utf-8', errors="ignore")
Can guess eg .decode('latin-1'),a module that can guess on encoding chardet.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 279 Jan-24-2024, 06:28 PM
Last Post: frohr
  how to save to multiple locations during save cubangt 1 509 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  save values permanently in python (perhaps not in a text file)? flash77 8 1,118 Jul-07-2023, 05:44 PM
Last Post: flash77
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 2,839 Feb-20-2023, 07:19 PM
Last Post: avd88
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,063 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Save multiple Parts of Bytearray to File ? lastyle 1 907 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,575 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 6,842 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  Deploy Python to Cloud and save output to Google Drive chandrabr80 2 1,523 Jan-25-2022, 06:56 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020