Shell re-direction

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Gould

    #1

    Shell re-direction

    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
  • Jeff Epler

    #2
    Re: Shell re-direction

    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)

    iD8DBQFCPgF6Jd0 1MZaTXX0RApsZAJ 9RXKs9I4I1RC/zLUJef+rRKrfeHQ CfcJh4
    AwRS1S2D684TTeG KLDRDYqg=
    =bzKZ
    -----END PGP SIGNATURE-----

    Comment

    Working...