Python Forum
python file output to log file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python file output to log file
#1
>python logzip.py > /var/cron/log
I need to capture of logzip.py output to /var/cron/log.
Above shows how to do in linux pmpt.

to achive this how to do in python ?
Reply
#2
Print function has a file option. The default is sys.stdout but you can replace it with an open file.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
With pathlib:
from pathlib import Path


def log(msg):
    cron_log = Path('/var/cron/log')
    with cron_log.open('r+') as log_fd:
        print(msg, file=log_fd)
Without pathlib
def log(msg):
    with open('/var/cron/log', 'r+') as log_fd:
        print(msg, file=log_fd)
A better solution is to use logging in Python: https://realpython.com/python-logging/#using-handlers
There are different solutions. You can create a handler to log to a file, you can log to syslog and systemd.
If you're not on Linux, you have lesser options.

The provided example has one problem. It opens the file, write to it and then the file is closed.
This happens for each message.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
that was a nice work of my query

however i have 2 more doubts on this,

1. will this msg arguments will pass all print output to logzip.py if we add this def(msg) ?
2. In print(msg, file=log_fd)getting sysntax error for file=log_fd !
Reply
#5

  1. You have to use the log('Your Message') everywhere, where you want to log. The print_function stays still the print_function and does not change.
  2. You're using Python 2.7, which is deprecated 2020. Upgrade to Python 3.7. In Python 2.7 print is not a function, it's a statement and the parenthesis around the arguments are recognized as a tuple and a tuple has no keyword assignment. If you need to stay with legacy Python, you should use the imports from __future__:
    from __future__ import print_function
    Then you must use everywhere print as a function

The logging module is doing a better job. You've more control about different handlers and formatting.
You could also try https://github.com/Delgan/loguru
The boiler-plate code has been removed :-)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to remove unwanted images and tables from a Word file using Python? rownong 2 707 Feb-04-2025, 08:30 AM
Last Post: Pedroski55
  Best way to feed python script of a file absolut 6 1,042 Jan-11-2025, 07:03 AM
Last Post: Gribouillis
  Removal of watermark logo pdf file Python druva 0 637 Jan-01-2025, 11:55 AM
Last Post: druva
  How to write variable in a python file then import it in another python file? tatahuft 4 860 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  How to communicate between scripts in python via shared file? daiboonchu 4 1,459 Dec-31-2024, 01:56 PM
Last Post: Pedroski55
  Problems writing a large text file in python Vilius 4 943 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  How to re-register .py file extension to new moved Python dir (on Windows)? pstein 5 1,228 Nov-06-2024, 03:06 PM
Last Post: DeaD_EyE
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,013 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  FileNotFoundError: [Errno 2] No such file or directory although the file exists Arnibandyo 0 802 Aug-12-2024, 09:11 AM
Last Post: Arnibandyo
  To fetch and iterate data from CSV file using python vyom1109 3 955 Aug-05-2024, 10:05 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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