Print to the same line and not a new line?

It’s called the carriage return, or \r Use print i/len(some_list)*100,” percent complete \r”, The comma prevents print from adding a newline. (and the spaces will keep the line clear from prior output) Also, don’t forget to terminate with a print “” to get at least a finalizing newline!

Capture stdout from a script?

For future visitors: Python 3.4 contextlib provides for this directly (see Python contextlib help) via the redirect_stdout context manager: from contextlib import redirect_stdout import io f = io.StringIO() with redirect_stdout(f): help(pow) s = f.getvalue()

How can I log the stdout of a process started by start-stop-daemon?

To expand on ypocat’s answer, since it won’t let me comment: start-stop-daemon –start –quiet –chuid $DAEMONUSER \ –make-pidfile –pidfile $PIDFILE –background \ –startas /bin/bash — -c “exec $DAEMON $DAEMON_ARGS > /var/log/some.log 2>&1” Using exec to run the daemon allows stop to correctly stop the child process instead of just the bash parent. Using –startas instead … Read more

Python: How to get stdout after running os.system? [duplicate]

If all you need is the stdout output, then take a look at subprocess.check_output(): import subprocess batcmd=”dir” result = subprocess.check_output(batcmd, shell=True) Because you were using os.system(), you’d have to set shell=True to get the same behaviour. You do want to heed the security concerns about passing untrusted arguments to your shell. If you need to … Read more

Force line-buffering of stdout in a pipeline

you can try stdbuf $ stdbuf –output=L ./a | tee output.txt (big) part of the man page: -i, –input=MODE adjust standard input stream buffering -o, –output=MODE adjust standard output stream buffering -e, –error=MODE adjust standard error stream buffering If MODE is ‘L’ the corresponding stream will be line buffered. This option is invalid with standard … Read more

How to capture stdout output from a Python function call?

Try this context manager: from io import StringIO import sys class Capturing(list): def __enter__(self): self._stdout = sys.stdout sys.stdout = self._stringio = StringIO() return self def __exit__(self, *args): self.extend(self._stringio.getvalue().splitlines()) del self._stringio # free up some memory sys.stdout = self._stdout Usage: with Capturing() as output: do_something(my_object) output is now a list containing the lines printed by the … Read more