Behavior of os.system() with redirection

Martin v. Loewis martin at v.loewis.de
Wed Jan 2 17:06:14 EST 2002


Aristotle_00 at yahoo.com (Mark) writes:

> What's going on here?  

sys.stdout is buffered. The subprocess completes first, and writes to
the file. Then python completes, and writes the sys.stdout buffer to
the file.

> How can I get a uniform ("hi" always first) behavior?

You can call sys.stdout.flush. Alternatively, you can invoke python
with -u, or set the PYTHONUNBUFFERED environment variable.

However, I would not recommend to invoke /bin/ls through
os.system. Instead, use os.listdir.

Regards,
Martin



More information about the Python-list mailing list