question for sys.stdout

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

    #1

    question for sys.stdout

    "stdout" is file object, it open mode is "w"
    and it have a close() fuction.....
    while it run close(), how to reopen it

    because I want to do rewrite it
    stdout content update


  • Pierre Barbier de Reuille

    #2
    Re: question for sys.stdout

    If you want to "rewrite" a "stdout" object, just write a file-like
    object and replace sys.stdout !

    Then don't care about the old stdout anymore ... it's a pipe and it
    won't bother you anymore :)

    Another way to do so is the "C" way ... ie. using os.dup2. The advantage
    of the "dup2" method is the replacement of the stdout of the whole
    program (so, if you use external functions writing in stdout it will
    work), but the drawback is you need to have a real file (ie. file, pipe,
    ....)

    Depends on what you want to use :)

    Leon a écrit :[color=blue]
    > "stdout" is file object, it open mode is "w"
    > and it have a close() fuction.....
    > while it run close(), how to reopen it
    >
    > because I want to do rewrite it
    > stdout content update
    >
    >[/color]

    Comment

    • Leon

      #3
      Re: question for sys.stdout

      sorry....
      how to replace sys.stdout to file-like object ?
      I use os module fdopen() and file object fileno()
      stdout = os.fdopen(sys.s tdout.fileno(), "w")
      or
      stdout = os.fdopen(os.du p(sys.stdout.fi leno()),"w")

      it still can't do rewrite, it result is append content

      ..>_<. Please help me

      "Pierre Barbier de Reuille" <pierre.barbier @cirad.fr>
      ???????:4189ec5 2$0$6282$626a14 ce@news.free.fr ...[color=blue]
      > If you want to "rewrite" a "stdout" object, just write a file-like object
      > and replace sys.stdout !
      >
      > Then don't care about the old stdout anymore ... it's a pipe and it won't
      > bother you anymore :)
      >
      > Another way to do so is the "C" way ... ie. using os.dup2. The advantage
      > of the "dup2" method is the replacement of the stdout of the whole program
      > (so, if you use external functions writing in stdout it will work), but
      > the drawback is you need to have a real file (ie. file, pipe, ...)
      >
      > Depends on what you want to use :)
      >
      > Leon a écrit :[color=green]
      >> "stdout" is file object, it open mode is "w"
      >> and it have a close() fuction.....
      >> while it run close(), how to reopen it
      >>
      >> because I want to do rewrite it
      >> stdout content update
      >>[/color][/color]

      Comment

      • Diez B. Roggisch

        #4
        Re: question for sys.stdout

        > how to replace sys.stdout to file-like object ?

        sys.stdout = open("/this/is/so/simple", "w")

        --
        Regards,

        Diez B. Roggisch

        Comment

        • Pierre Barbier de Reuille

          #5
          Re: question for sys.stdout

          MMmhh ... I'm not sure what you _really_ want to do ???

          You want to replace stdout by a custom file-like object ? Because I
          don't see what you call "doing rewrite" ... You should give a clear
          example of the behaviour you want.

          Pierre

          Leon a écrit :[color=blue]
          > sorry....
          > how to replace sys.stdout to file-like object ?
          > I use os module fdopen() and file object fileno()
          > stdout = os.fdopen(sys.s tdout.fileno(), "w")
          > or
          > stdout = os.fdopen(os.du p(sys.stdout.fi leno()),"w")
          >
          > it still can't do rewrite, it result is append content
          >
          > .>_<. Please help me
          >
          > "Pierre Barbier de Reuille" <pierre.barbier @cirad.fr>
          > ???????:4189ec5 2$0$6282$626a14 ce@news.free.fr ...
          >[color=green]
          >>If you want to "rewrite" a "stdout" object, just write a file-like object
          >>and replace sys.stdout !
          >>
          >>Then don't care about the old stdout anymore ... it's a pipe and it won't
          >>bother you anymore :)
          >>
          >>Another way to do so is the "C" way ... ie. using os.dup2. The advantage
          >>of the "dup2" method is the replacement of the stdout of the whole program
          >>(so, if you use external functions writing in stdout it will work), but
          >>the drawback is you need to have a real file (ie. file, pipe, ...)
          >>
          >>Depends on what you want to use :)
          >>
          >>Leon a écrit :
          >>[color=darkred]
          >>>"stdout" is file object, it open mode is "w"
          >>>and it have a close() fuction.....
          >>>while it run close(), how to reopen it
          >>>
          >>>because I want to do rewrite it
          >>>stdout content update
          >>>[/color][/color]
          >
          >[/color]

          Comment

          Working...