![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
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.
|
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
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") |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
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() |
|
|
|
|
|
#4 |
|
Expert Programmer
|
Thanks. However, this script is actaully running on a windows machine. I tried the following:
sys.stdout = open('C:\junk.txt', 'w')EDIT: The Blackhole technique doesn't seem to work either. Thanks though. |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Try doing the same thing with sys.stderr instead.
|
|
|
|
|
|
#6 |
|
Expert Programmer
|
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|