Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console warnings to file

Tags:

python

I am running a python script using pandas.read_csv to import a csv file. Pandas provides console warnings when it doesn't see what it expects such as:

Skipping line 163: Expected 41 fields in line 163, saw 42

How can I log this to a text file?

If I run the script from a command line, python > logfile.txt only the output of print shows up in the file, not the warnings.

like image 601
Michael C Avatar asked Jun 25 '26 04:06

Michael C


1 Answers

Use stderr

In bash, there are 3 default streams:

STDOUT: Redirected by > or 1>, this is standard output from a program.

STDERR: Redirected by 2>, this is diagnostic output from a program.

STDIN: Input from console, use < to input it.


./prog 2> errorlog.txt >logfile.txt

If you want to redirect ALL output to STDOUT, use:

./prog 2>&1

Here is some more information: I/O Redirection

like image 100
blackbrandt Avatar answered Jun 27 '26 17:06

blackbrandt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!