Log divert to txt file

Hi Every one,
I’m new to python. I’m trying to divert the complete operational(detail) log in python to a text file but I’m unable to find out how to do it. Could you help me how to achieve it.
ex:
{code}
import sys
a=10
b=20

Looking for log:
Importing sys
a=10
b=20

Hi Saman,

can you please give us some details? Maybe even some screenshots? At the moment I just can vaguely guess what you want and I don’t even know whether you are working on Linux, Windows, macOS, …?

Cheers, Dominik

Sorry for unclear message. I want whole operation to be moved to the txt file. Reason is we created a batch file to run the python code from Control-M. When the job fails we are not able to find the reason for failure. Again we are opening the code manually and executing and finding the error. Attached screenshot for one of the error which I made it to show what I’m looking for. (as the codes are in prod region I could not take screenshot of that). If I made this error move to some external file (txt,log) it will be easy to do RCA and fix the code.
The codes are running in Windows10 Machine

When an exception occurs, Python prints a so-called “traceback” with all the lines of code that eventually led to the error. For example:

def bad():
    1/0 # Whoops!

def call_bad():
    bad()

def call_call_bad():
    call_bad()

call_call_bad()

The error printed for this is:

Traceback (most recent call last):
  File "<tmp 1>", line 11, in <module>
    call_call_bad()
  File "<tmp 1>", line 8, in call_call_bad
    call_bad()
  File "<tmp 1>", line 5, in call_bad
    bad()
  File "<tmp 1>", line 2, in bad
    1/0 # Whoops!
ZeroDivisionError: division by zero

This is usually enough. Python does not keep track of every line of code executed because it would be very large to hold in memory and impact performance. I never felt the need for it, either: the amount of output would be too huge for any non-trivial program that it would be unreadable. When you want to debug a program, you have two main options:

  • insert print() calls at strategic places,
  • use a debugger, such as pdb.

I probably don’t get it all but nevertheless some links:

  • to capture error messages there is contextlib.redirect_stderr (‘redirecting sys.stderr to another file or file-like object.’).
  • In ipython there is also cell magic %%capture (‘run the cell, capturing stdout, stderr, and IPython’s rich display() calls.’)

You’re looking for Python’s logging module: logging — Logging facility for Python — Python 3.9.5 documentation

It is designed almost explicitly for this particular case.

Hi Saman,

you can redirect (error) output in the python script as Aivar has mentioned:

as well as you can do so from CMD resp Power Shell, e.g.:

After redirecting the stderr to a file and after execution you then can find and interprete the

Jean has mentioned in said file.

Is this what you were looking for?

Cheers, Dominik

Thank you every one for your precious time. Will try these options and let you know.

1 Like