redirecting print to a a file

A

AndyL

Hi,

Can I redirect print output, so it is send to a file, not stdout.

I have a large program and would like to avoid touching hundreds of print's.

Thx, Andy
 
A

AndyL

And what if I want to still send the output to stdout and just a log it
in the file as well?

A.
 
A

AndyL

Sybren said:
I can suggest using the logging module instead of print. It's much
more flexible than prints, and well suited for large programs.

Thx for the hint. I will look into that.

A.
 
A

AndyL

Sybren said:
AndyL enlightened us with:



$ python some_program.py | tee output.log

Or write a class that has a write() function and outputs to a file and
to the original value of sys.stdout (IIRC that's in sys.__stdout__)

Sybren

Thx again. Python is cool, do that in C++ or Java :)

A.
 
S

Sybren Stuvel

AndyL enlightened us with:
Can I redirect print output, so it is send to a file, not stdout.

Change sys.stdout to a file object.
I have a large program and would like to avoid touching hundreds of
print's.

I can suggest using the logging module instead of print. It's much
more flexible than prints, and well suited for large programs.

Sybren
 
S

Sybren Stuvel

AndyL enlightened us with:
And what if I want to still send the output to stdout and just a log
it in the file as well?

$ python some_program.py | tee output.log

Or write a class that has a write() function and outputs to a file and
to the original value of sys.stdout (IIRC that's in sys.__stdout__)

Sybren
 
M

Mirco Wahab

AndyL said:
Thx again. Python is cool, do that in C++ or Java :)

Yes, thats obviously true ;-)

$> javac someprogram.java;
$> java someprogram | tee output.log

$> g++ someother.cpp -o someother
$> ./someother | tee output.log

SCNR,

Mirco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,529
Members
48,240
Latest member
เพิ่มไลค์|LikePro

Latest Threads

Top