sys.stdout .>_<.

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

    #1

    sys.stdout .>_<.

    import os,sys
    a = os.fdopen(sys.s tdout.fileno(), "w")
    b = os.fdopen(sys.s tdout.fileno(), "w")
    a.write("test1 ")
    b.write("test2 ")

    stdout result is

    test1 test2

    but use general file object such as...

    a = open("test.txt" ,"w")
    b = open("test.txt" ,"w")
    a.write("test1" )
    b.write("test2" )

    the "test.txt" file content is

    test2

    how to do this result to sys.stdout


  • Pierre Barbier de Reuille

    #2
    Re: sys.stdout .&gt;_&lt;.

    It's not possible !

    stdout is usually a pipe ... and you cannot go back in a pipe ...
    Try with any pipe you should have the same result.

    The only way toi achieve this is to assume an ANSI terminal and to use
    ANSI codes to go back ("\r" to go at the beginning of the line "^h" to
    go back one char)

    Leon a écrit :[color=blue]
    > import os,sys
    > a = os.fdopen(sys.s tdout.fileno(), "w")
    > b = os.fdopen(sys.s tdout.fileno(), "w")
    > a.write("test1 ")
    > b.write("test2 ")
    >
    > stdout result is
    >
    > test1 test2
    >
    > but use general file object such as...
    >
    > a = open("test.txt" ,"w")
    > b = open("test.txt" ,"w")
    > a.write("test1" )
    > b.write("test2" )
    >
    > the "test.txt" file content is
    >
    > test2
    >
    > how to do this result to sys.stdout
    >
    >[/color]

    Comment

    • Mike Meyer

      #3
      Re: sys.stdout .&gt;_&lt;.

      [Format repaired]

      Pierre Barbier de Reuille <pierre.barbier @cirad.fr> writes:[color=blue]
      > Leon a écrit :[color=green]
      >> import os,sys
      >> a = os.fdopen(sys.s tdout.fileno(), "w")
      >> b = os.fdopen(sys.s tdout.fileno(), "w")
      >> a.write("test1 ")
      >> b.write("test2 ")
      >> stdout result is
      >> test1 test2
      >> but use general file object such as...
      >> a = open("test.txt" ,"w")
      >> b = open("test.txt" ,"w")
      >> a.write("test1" )
      >> b.write("test2" )
      >> the "test.txt" file content is
      >> test2
      >> how to do this result to sys.stdout[/color]
      > It's not possible ![/color]

      Yup.
      [color=blue]
      > stdout is usually a pipe ... and you cannot go back in a pipe ...
      > Try with any pipe you should have the same result.[/color]

      Actually, his second example didn't "go back". The two file pointers
      started at the beginning of the file. The first write writes "test1"
      into the file starting at the beginning. The second write writes
      "test2" at the file starting at the beginning, overwriting the results
      of the first write.
      [color=blue]
      > The only way toi achieve this is to assume an ANSI terminal and to use
      > ANSI codes to go back ("\r" to go at the beginning of the line "^h" to
      > go back one char)[/color]

      The other altnerative is the curses module.

      <mike
      --
      Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
      Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

      Comment

      Working...