By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

Shell re-direction

P: n/a
Hi all,
I have a very strange problem with the output run from commands run via
os.system(). If the python script is run without re-direction the
output appears as expected, but if I re-direct the output from the
script the output gets re-ordered.

For example given the following script:

import os
print '1'
os.system('echo 2')
print '3'

$ python test.py
1
2
3
$ python test.py | cat
2
1
3

Can anyone explain what's going on here?

Thanks
Mike
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
buffering.

In the first case, there is either no buffering, or line buffering on
sys.stdout, so you see the lines in order.

In the second case, there is a buffer of a few hundred or thousand bytes
for stdout in the python process, and you see the two lines of python
output together (in this case, when the python process exits and flushes
all its buffers).

You can use the "flush" method on file objects to "clear out" these
buffers.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCPgF6Jd01MZaTXX0RApsZAJ9RXKs9I4I1RC/zLUJef+rRKrfeHQCfcJh4
AwRS1S2D684TTeGKLDRDYqg=
=bzKZ
-----END PGP SIGNATURE-----

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.