can we save print msg into a file when script running ?

B

black

hi all~

in my .py file there are a few print to trace out some message and i
wonder if we can save it into a specified file when that script get
running. if so, i may just check that file to c how the script is
running. can anyone show me a right direction ?

one million tks~
 
S

Steve Holden

black said:
hi all~

in my .py file there are a few print to trace out some message and i
wonder if we can save it into a specified file when that script get
running. if so, i may just check that file to c how the script is
running. can anyone show me a right direction ?

one million tks~
On Unix platforms you can run your script with a redirection of the
standard output:

python myscript.py > somefile.txt &

[The ampersand runs the command in the background]. Then you can monitor
the content of the output file with

tail -f somefile.txt

which will show you the last part of the file and further additions as
long as you let the tail command run.

regards
Steve
 
M

Maksim Kasimov

black said:
hi all~

in my .py file there are a few print to trace out some message and i
wonder if we can save it into a specified file when that script get
running. if so, i may just check that file to c how the script is
running. can anyone show me a right direction ?

one million tks~

script.py >> script.log 2>&1
 
B

black

quote:
=======================
script.py >> script.log 2>&1
=======================

what does 2>&1 mean pls ?
 
P

Peter Otten

black said:
in my .py file there are a few print to trace out some message and i
wonder if we can save it into a specified file when that script get
running. if so, i may just check that file to c how the script is
running. can anyone show me a right direction ?

If you don't feel comfortable with the command line you can stick

import sys
sys.stdout = open("mylogfile.txt", "w")

at the beginning of your file.

Peter
 
S

Steve Holden

black said:
quote:
=======================
script.py >> script.log 2>&1
=======================

what does 2>&1 mean pls ?
It's Unix shell-speak for "send the standard error stream to the same
place as the standard output". Probably a syntax error on Windows ...

regards
Steve
 
F

Fredrik Lundh

Steve said:
It's Unix shell-speak for "send the standard error stream to the same
place as the standard output". Probably a syntax error on Windows ...
more test.py
import sys
sys.stdout.write("stdout!\n")
sys.stderr.write("stderr!\n")
python test.py >out stderr!

python test.py 2>out stdout!

python test.py >out 2>&1
more out stdout!
stderr!

ver
Microsoft Windows XP [Version 5.1.2600]

</F>
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,088
Latest member
JeremyMedl

Latest Threads

Top