The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Stdout issue in python
Discuss Stdout issue in python in the Python Programming forum on Dev Shed. Stdout issue in python Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 31st, 2012, 01:17 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 9
Time spent in forums: 1 h 42 m 30 sec
Reputation Power: 0
|
|
|
Stdout issue in python
Hi,
I am working on a script. Requirement is to divert all output to a file. I did this with below code-:
sys.stdout=open(output_file_name,'w')
Now, after execution of the script i want to print one line to console that, please check the output in "output_file_name".
But this line also get printed inside the file instead of console. Can any one help?
Regards,
Sachin Bali
|

December 31st, 2012, 01:38 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
Back up the old sys.stdout into another variable before you make it write to a file:
Code:
old_stdout = sys.stdout
sys.stdout=open(output_file_name,'w')
# Do stuff here
old_stdout.write("Something that goes to the screen\n")
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
|

December 31st, 2012, 02:15 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 9
Time spent in forums: 1 h 42 m 30 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Scorpions4ever Back up the old sys.stdout into another variable before you make it write to a file:
Code:
old_stdout = sys.stdout
sys.stdout=open(output_file_name,'w')
# Do stuff here
old_stdout.write("Something that goes to the screen\n")
|
Yes,
it is working. Thanks.
But can you explain the reason why we need to store the sys.stdout to some other variable ?
Regards,
Sachin Bali
|

December 31st, 2012, 07:25 AM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 74
  
Time spent in forums: 1 Day 2 h 22 m 37 sec
Reputation Power: 2
|
|
|
You can also use sys.__stdout__ which is supposed to contain the "original" value of sys.stdout as of the start of the program.
|

December 31st, 2012, 08:26 AM
|
 |
Contributing User
|
|
|
|
>>> a = 43
>>> a = 'a string'
>>> # how will you recover 43?
In recent python2 there's some print syntax that directs output to some file not stdout.
In python3 print is a function that accepts a keyword file destination.
Code:
>>> help(print)
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
Initially you might think as our group did that redirecting the print sink and input source are wonderful mechanisms for tests and whatnot. Later you'll realize that python has a wonderful StringIO class and that many functions are better off returning strings rather than directly printing them, and to separate opening files from using them. Instead of passing a file name argument pass open file-like objects.
__________________
[code] Code tags[/code] are essential for python code!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|