Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 30th, 2006, 2:17 PM   #1
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 908
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Suppressing output

Is there a way to suppress output to stdout in Python? I am importing a module written by someone else that I do not want to modify which prints status messages while it is running. Is there an easy way to prevent these messages from being printed, then allowing my program to print its own messages? I assume the solution to this would be to redirect stdout, which I might look into, but is that the easiest/standard way to do this? Thanks.
titaniumdecoy is offline   Reply With Quote
Old Jun 30th, 2006, 2:29 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
I'm unfamiliar with OS X filesystems, but since they're Unix-based, presumably they have some manner of "black hole" file that swallows up all data piped to it. Under Linux, /dev/null fulfils this function.
import sys
output = sys.stdout
sys.stdout = open("/dev/null", "w")
print "Hello"
output.write("Hello\n")
There may also be a more crossplatform way of handling the problem. Perhaps by creating a pipe with os.pipe and redirecting sys.stdout to it. But then one may have problem of the pipe buffer growing too large.
Arevos is offline   Reply With Quote
Old Jun 30th, 2006, 2:38 PM   #3
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
I've also discovered this technique, which works on all variations of "print" I could think of, but I can't be sure if it's completely foolproof:
class Blackhole:
    def write(self, *args):
        pass

sys.stdout = Blackhole()
Arevos is offline   Reply With Quote
Old Jun 30th, 2006, 2:43 PM   #4
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 908
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Thanks. However, this script is actaully running on a windows machine. I tried the following:

sys.stdout = open('C:\junk.txt', 'w')
But it doesn't seem to make any difference. I've tried sys.stdout = '' and a bunch of other things, but when I type sys.stdout, it always prints <wx.py.pseudo.PseudoFileOut instance at 0x01BC25A8>. For some reason I am unable to change the sys.stdout variable, or so it would seem.

EDIT: The Blackhole technique doesn't seem to work either. Thanks though.
titaniumdecoy is offline   Reply With Quote
Old Jun 30th, 2006, 2:46 PM   #5
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Try doing the same thing with sys.stderr instead.
Arevos is offline   Reply With Quote
Old Jun 30th, 2006, 2:48 PM   #6
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 908
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Oops, I put the Blackhole code too late in my code. It works now, thanks.

Last edited by titaniumdecoy; Jun 30th, 2006 at 3:05 PM.
titaniumdecoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:51 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC