![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,827
Rep Power: 5
![]() |
Redirecting Console Output
I need to redirect all console output (including error messages) to a log file. There are several reasons why I would have to go out of my way to change each print statement to a call to a function. It would be much easier if there's some support in the sys library for just that?
Any helpful info? |
|
|
|
|
|
#2 | |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
Quote:
import sys
oldstdout = sys.stdout # So you can restore later
sys.stdout = file("C:/temp/test.txt", "w") # Or whatever your logfile is
for i in range(10):
print "Hello", i
sys.stdout.close()
sys.stdout = oldstdoutAnother alternative would be to import logging and actually log things properly rather than using prints. -T. |
|
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
You generally do this from the command line. Not sure if this is the same with windows (doubtful) but under Linux (with BASH) you generally do
python foo.py > logfile.txt 2>&1 If you're writing to stderr from your Python program then you want to just set sys.stderr to sys.stdout at the top of your source file, which will effectively 'redirect' everything to STDOUT. |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Dec 2004
Location: UK
Posts: 53
Rep Power: 4
![]() |
The "> file.ex" thing does work in windows as does the "< input.ex" for providing automatic input. Didnt know about the "2>&1" ... where abouts can I find some documentation on it, Cerulean?
__________________
Tetris is so unrealistic |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|