Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 31st, 2012, 01:17 AM
Sachin Bali Sachin Bali is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 9 Sachin Bali User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old December 31st, 2012, 01:38 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,382 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 20 h 31 m 48 sec
Reputation Power: 4080
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

Reply With Quote
  #3  
Old December 31st, 2012, 02:15 AM
Sachin Bali Sachin Bali is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 9 Sachin Bali User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old December 31st, 2012, 07:25 AM
Nyktos Nyktos is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 74 Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level) 
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.

Reply With Quote
  #5  
Old December 31st, 2012, 08:26 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is online now
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,350 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 38 m 45 sec
Reputation Power: 383
>>> 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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Stdout issue in python

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap