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)
#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


Messages In This Thread
RE: Save console output to text file (like a log) - by snippsat - Jun-02-2019, 06:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 360 Jan-24-2024, 06:28 PM
Last Post: frohr
  how to save to multiple locations during save cubangt 1 592 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  save values permanently in python (perhaps not in a text file)? flash77 8 1,284 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,152 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 3,160 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,165 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Save multiple Parts of Bytearray to File ? lastyle 1 982 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,745 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 7,089 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  Deploy Python to Cloud and save output to Google Drive chandrabr80 2 1,622 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