[Tutor] Collecting output from Python scripts executed via Cron
akleider at sonic.net
Fri May 19 11:52:21 EDT 2017
On 2017-05-18 18:48, Leo Silver wrote:
> I have written a several Python scripts to collect data from external
> sources (an email account and an sftp site).
> In development I run the scripts from IDLE or the command line and can
> the output of various print statements in the scripts which helps me to
> monitor progress and confirm correct operation.
> However, in production I will be running the scripts via Cron.
> Is there a recommended/ elegant way to collect this output on the fly
> later review/ processing.
From the python documentation of the print function:
print([object, ...], *, sep=' ', end='\n', file=sys.stdout)
Print object(s) to the stream file, separated by sep and followed by
end. sep, end and file, if present, must be given as keyword arguments.
All non-keyword arguments are converted to strings like str() does
and written to the stream, separated by sep and followed by end. Both
sep and end must be strings; they can also be None, which means to use
the default values. If no object is given, print() will just write end.
The file argument must be an object with a write(string) method; if
it is not present or None, sys.stdout will be used.
The following might work:
>>> with open("progress.txt", "a") as outf:
print("what ever you used to print", file=outf)
> Previously I have written bash scripts simply redirecting the standard
> output to a text file and emailed this back to myself but I would like
> do this directly within Python rather than having to wrap the Python
> in a bash script.
> Thanks, Leo.
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
More information about the Tutor