os.popen output different from native shell output

Nobody nobody at nowhere.com
Wed Aug 26 16:18:28 CEST 2009

On Tue, 25 Aug 2009 11:42:31 -0700, nickname wrote:

> The reason why I want to do this is because I am going to do a little
> project. I will write a python script called ls which will log the
> time and username and then will show the actual ls output. I want this
> to be transparent and so want to throw the ls output (via python)
> exactly as it will be in native shell execution.

If your script doesn't need the "ls" output for its own purposes, just run
"ls" with its output to the terminal, rather than into a pipe. I.e. don't
use os.popen(), but use os.spawnvp(), os.execvp() or subprocess.call().

If running "ls" is the very last thing which your script does, use
os.execvp(). This causes the program to be executed in the existing
process, rather than spawning a child process and waiting for it to exit.

Because they replace the existing program (the Python interpreter), the
various os.exec* functions don't return (unless there's an error). If you
need to perform any action after the command completes (e.g. log the
execution time), you need to use one of the other functions instead.

More information about the Python-list mailing list