Linked Questions
36 questions linked to/from Temporarily Redirect stdout/stderr
-1 votes
1 answer
536 views
Python IO: disable standard output stream and print what we want [duplicate]
Say I have a python program composed of three lines. if __name__=“__main__”: foo( ) print “Hello” Here, foo( ) is a third-part function that outputs things to the standard output. E.g, foo( ) ...
0 votes
1 answer
282 views
In Python, how can the console output of a selection of code be suppressed? [duplicate]
I have a small selection of code: stream = pyaudio.PyAudio().open( format = pyaudio.PyAudio().get_format_from_width(1), channels = 1, rate = bitrate, output = True ) When I ...
447 votes
15 answers
816k views
Redirect stdout to a file in Python? [duplicate]
How do I redirect stdout to an arbitrary file in Python? When a long-running Python script (e.g, web application) is started from within the ssh session and backgounded, and the ssh session is closed,...
85 votes
8 answers
95k views
Silence the stdout of a function in Python without trashing sys.stdout and restoring each function call
Is there a way in Python to silence stdout without wrapping a function call like following? Original Broken Code: from sys import stdout from copy import copy save_stdout = copy(stdout) stdout = ...
50 votes
2 answers
25k views
I want Python argparse to throw an exception rather than usage
I don't think this is possible, but I want to handle exceptions from argparse myself. For example: import argparse parser = argparse.ArgumentParser() parser.add_argument('--foo', help='foo help', ...
29 votes
6 answers
22k views
Python stats: how do I write it to a (human readable) file
I am using Python's hotshot profiler: http://docs.python.org/2/library/hotshot.html It shows how to print the stats: stats.print_stats(20) But how do I get that into a file? I'm not sure how to get ...
12 votes
3 answers
16k views
In python, can I redirect the output of print function to stderr?
There're lots of print function (python 2.7) in my program. Is there any way I can add a few lines then all the output can be redirected to stderr? What I want is python codes but not linux pipeline. ...
5 votes
4 answers
20k views
python os.mkfifo() for Windows
Short version (if you can answer the short version it does the job for me, the rest is mainly for the benefit of other people with a similar task): In python in Windows, I want to create 2 file ...
11 votes
2 answers
12k views
Pandas Bad Lines Warning Capture
Is there any way in Pandas to capture the warning produced by setting error_bad_lines = False and warn_bad_lines = True? For instance the following script: import pandas as pd from StringIO import ...
4 votes
3 answers
11k views
How to log smtp debug information to a file?
As you know, python smtplib has a debug level.When I set it a true param, it will print some send information. The problem is, I try to get the debug info to log into a file, but They just stay on my ...
1 vote
2 answers
8k views
How to pipe the output of a command to `/dev/null` in python
I am trying to pipe the output of the following command to /dev/null so that it doesn't get printed for the user that is running the program. import os os.system("ping -c 1 192.168.unknown.host > /...
10 votes
1 answer
2k views
why does sys.stdout = None work?
i can silence and restore sys.stdout this way: import sys sys.stdout = None print('hello') # does not write to stdout sys.stdout = sys.__stdout__ print('hello') # writes to stdout i know i'd better ...
4 votes
3 answers
2k views
sys.stdout as default function argument
Suppose we have the following dummy function: import sys def writeline(text, stream=sys.stdout): stream.write(text + '\n') with open('/path/to/file', 'w') as f: # writes to /path/to/file ...
5 votes
1 answer
3k views
How do I capture stderr in Python?
I would like to capture error messages which are generated from a python module A. A is written in C++ and SWIG, and so I cannot capture Python's sys.stderr. >>> import A >>> A.func(...
2 votes
2 answers
2k views
Equivalent funcion of PHP "ob" functions in Python/Django
Is there any function do the same result like PHP ob_start(myCallbackFunction) and ob_end_flush() that allow me modify the layouts and views in Python frameworks (Django/others)? thanks! UPDATE <?...