Bottom Page

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!
Quote
#2
check logging module. you can specify both console and file handler(s)
Quote
#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!
Quote
#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
Quote
#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
Quote
#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.
Quote
#7
https://stackoverflow.com/questions/4675...-in-python
Quote
#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
Quote
#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
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Read and save file in chucksize zinho 3 114 Apr-02-2020, 08:12 AM
Last Post: DeaD_EyE
  Import Text, output curve geometry Alyner 0 189 Feb-03-2020, 03:05 AM
Last Post: Alyner
  csv file output rturus 7 218 Jan-30-2020, 02:09 PM
Last Post: buran
  Rename file from value in text file Nuge93 1 170 Jan-20-2020, 03:50 PM
Last Post: gruntfutuk
  File name as part of output file name Jeeping_Coder 1 170 Jan-10-2020, 03:43 PM
Last Post: Clunk_Head
  Save a file uploaded from client-side without having to read into memory andym118 3 469 Nov-21-2019, 07:34 AM
Last Post: DeaD_EyE
  Details of attachment files in a msg file such as file names save into a python list klllmmm 2 602 Nov-12-2019, 05:59 AM
Last Post: klllmmm
  How to extract a matrix from .xml.gz file to a excel file or any other output? enyrb 0 207 Oct-21-2019, 01:01 PM
Last Post: enyrb
  Read each line, replace string and save into a new file igormonteiro 2 495 Sep-15-2019, 01:24 PM
Last Post: buran
  save content of table into file atlass218 10 841 Aug-28-2019, 12:12 PM
Last Post: Gribouillis

Forum Jump:


Users browsing this thread: 1 Guest(s)