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
  Can rich.logging output to file? pyfoo 1 239 Mar-21-2024, 10:30 AM
Last Post: pyfoo
  Python openyxl not updating Excel file MrBean12 1 312 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 390 Feb-15-2024, 11:15 AM
Last Post: rawatg
  connect sql by python using txt. file dawid294 2 426 Jan-12-2024, 08:54 PM
Last Post: deanhystad
  file open "file not found error" shanoger 8 1,084 Dec-14-2023, 08:03 AM
Last Post: shanoger
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,426 Nov-09-2023, 10:56 AM
Last Post: mg24
  Replace a text/word in docx file using Python Devan 4 3,286 Oct-17-2023, 06:03 PM
Last Post: Devan
  Help creating shell scrip for python file marciokoko 10 1,346 Sep-16-2023, 09:46 PM
Last Post: snippsat
  Need to replace a string with a file (HTML file) tester_V 1 757 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Python Launcher INI File leodavinci1990 1 813 Jul-30-2023, 03:38 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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