redirection in a file with os.system

Discussion in 'Python' started by TP, Nov 3, 2008.

  1. TP

    TP Guest

    Hi everybody,

    The following code does not redirect the output of os.system("ls") in a
    file:

    import sys, os
    saveout = sys.stdout
    fd = open( 'toto', 'w' )
    sys.stdout = fd
    os.system( "ls" )
    sys.stdout = saveout
    fd.close()

    Whereas the following works:

    old_stdout = os.dup( sys.stdout.fileno() )
    fd = os.open( 'bar', os.O_CREAT | os.O_WRONLY )
    os.dup2( fd, sys.stdout.fileno() )
    os.system( "ls" )
    os.close( fd )
    os.dup2( old_stdout, sys.stdout.fileno() )

    Why?

    I have another question: with this last code using os.open, the problem is
    that the file 'bar' is not removed before being written. So, it could lead
    to errors: the file 'bar' is overwritten, but extra lines from previous
    executions could remain.
    Am I compelled to use os.unlink (or os.remove) before calling
    os.system("ls")?

    Thanks

    Julien

    --
    python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+,\'Z
    (55l4('])"

    "When a distinguished but elderly scientist states that something is
    possible, he is almost certainly right. When he states that something is
    impossible, he is very probably wrong." (first law of AC Clarke)
    TP, Nov 3, 2008
    #1
    1. Advertising

  2. TP <> writes:

    > Hi everybody,
    >
    > The following code does not redirect the output of os.system("ls") in a
    > file:
    >
    > import sys, os
    > saveout = sys.stdout
    > fd = open( 'toto', 'w' )
    > sys.stdout = fd
    > os.system( "ls" )
    > sys.stdout = saveout
    > fd.close()
    >
    > Whereas the following works:
    >
    > old_stdout = os.dup( sys.stdout.fileno() )
    > fd = os.open( 'bar', os.O_CREAT | os.O_WRONLY )
    > os.dup2( fd, sys.stdout.fileno() )
    > os.system( "ls" )
    > os.close( fd )
    > os.dup2( old_stdout, sys.stdout.fileno() )
    >
    > Why?
    >
    > I have another question: with this last code using os.open, the problem is
    > that the file 'bar' is not removed before being written. So, it could lead
    > to errors: the file 'bar' is overwritten, but extra lines from previous
    > executions could remain.
    > Am I compelled to use os.unlink (or os.remove) before calling
    > os.system("ls")?


    Do you have to use low level os functions? Why not use the subprocess
    module? E.g

    >>> subprocess.call('ls', stdout=open('toto', 'w')


    HTH

    --
    Arnaud
    Arnaud Delobelle, Nov 3, 2008
    #2
    1. Advertising

  3. TP

    Guest

    On Nov 4, 12:06 am, TP <> wrote:
    > Hi everybody,
    >
    > The following code does not redirect the output of os.system("ls") in a
    > file:
    >
    > import sys, os
    > saveout = sys.stdout
    > fd = open( 'toto', 'w' )
    > sys.stdout = fd
    > os.system( "ls" )
    > sys.stdout = saveout
    > fd.close()


    os.system() will call the libc system() which should fork() and exec()
    the '/bin/sh' shell with your command. The shell will inherit python's
    file descriptors. sys.stdout is a python level object, not a process
    level descriptor. By swapping sys.stdout with another file object you
    have only changed a python level file object. In the second snippet
    you have correctly updated the underlying process level descriptors.

    I imagine a "print" statement just after the "sys.stdout = fd" will
    not go to your stdout but the 'toto' file.

    -srp



    >
    > Whereas the following works:
    >
    > old_stdout = os.dup( sys.stdout.fileno() )
    > fd = os.open( 'bar', os.O_CREAT | os.O_WRONLY )
    > os.dup2( fd, sys.stdout.fileno() )
    > os.system( "ls" )
    > os.close( fd )
    > os.dup2( old_stdout, sys.stdout.fileno() )
    >
    > Why?
    >
    > I have another question: with this last code using os.open, the problem is
    > that the file 'bar' is not removed before being written. So, it could lead
    > to errors: the file 'bar' is overwritten, but extra lines from previous
    > executions could remain.
    > Am I compelled to use os.unlink (or os.remove) before calling
    > os.system("ls")?
    >
    > Thanks
    >
    > Julien
    >
    > --
    > python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+,\'Z
    > (55l4('])"
    >
    > "When a distinguished but elderly scientist states that something is
    > possible, he is almost certainly right. When he states that something is
    > impossible, he is very probably wrong." (first law of AC Clarke)
    , Nov 4, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Terry Gray

    os.system stdout redirection...

    Terry Gray, Aug 17, 2003, in forum: Python
    Replies:
    8
    Views:
    3,175
    baiste
    Oct 11, 2003
  2. Replies:
    7
    Views:
    383
    Herbert Rosenau
    Aug 2, 2006
  3. Rohit Srivastava

    File System Redirection Windows x 64 box.

    Rohit Srivastava, May 15, 2009, in forum: Python
    Replies:
    2
    Views:
    683
    Dave Angel
    May 15, 2009
  4. Gennady
    Replies:
    0
    Views:
    177
    Gennady
    Jun 28, 2003
  5. James T Kirk
    Replies:
    2
    Views:
    133
    James T Kirk
    Aug 21, 2003
Loading...

Share This Page