Clearing file object buffers

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

    Clearing file object buffers

    In my code i have setup sys.stdout and sys.stderr as file objects so that i
    can easily track whats going on during the course of my program. So far its
    proving useful but the objects seem to be buffering any text supplied to
    their write() functions and when i hit an error condition and have to exit
    early they fail to write and i'm left with empty files.

    Is there any way to force the file objects to flush the buffer to file on
    disc?

    thanks,
    dis


  • Peter Hansen

    #2
    Re: Clearing file object buffers

    disgracelands wrote:[color=blue]
    >
    > In my code i have setup sys.stdout and sys.stderr as file objects so that i
    > can easily track whats going on during the course of my program. So far its
    > proving useful but the objects seem to be buffering any text supplied to
    > their write() functions and when i hit an error condition and have to exit
    > early they fail to write and i'm left with empty files.
    >
    > Is there any way to force the file objects to flush the buffer to file on
    > disc?[/color]

    fileobject.flus h() will do the trick.

    -Peter

    Comment

    • John J. Lee

      #3
      Re: Clearing file object buffers

      "disgracela nds" <disgracelands@ quietblue.co.uk > writes:
      [color=blue]
      > In my code i have setup sys.stdout and sys.stderr as file objects so that i
      > can easily track whats going on during the course of my program. So far its
      > proving useful but the objects seem to be buffering any text supplied to
      > their write() functions and when i hit an error condition and have to exit
      > early they fail to write and i'm left with empty files.
      >
      > Is there any way to force the file objects to flush the buffer to file on
      > disc?[/color]

      python's -u command-line switch during debugging can be useful.


      John

      Comment

      Working...