Flushing sdtout

Bjorn Pettersen bjorn at roguewave.com
Fri Jul 7 15:04:20 EDT 2000


...or use a class

class FlushFile:
    def __init__(self, f):
        self.f = f

    def write(self, txt):
        self.f.write(txt)
        self.f.flush()

sys.stdout = FlushFile(sys.stdout)

print "this string is autoflushed :-)"

-- bjorn

Olivier Dagenais wrote:
> 
> Do a blank raw_input, sort of like:
> 
>     print 'Enter type of DRC to run from menu -->'
>     sys.stdout.flush ( )
>     selection = raw_input ( ' ' )
> 
> Also, you could write your own function:
> 
> def print_flush ( message ):
>     print message
>     sys.stdout.flush ( )
> 
> --
> ----------------------------------------------------------------------
> Olivier A. Dagenais - Carleton University - Computer Science III
> 
> "Aaron Ginn" <aaron.ginn at motorola.com> wrote in message
> news:sn4s61hl9t.fsf at motorola.com...
> >
> > Is there a simple way to flush stdout on every print statement similar
> > to the $| variable in perl?  Currently, I'm adding a
> > sys.stdout.flush() statement after every single print statement in a
> > script in order to write the run to a logfile, but I'd like to simply
> > be able to tell python to do it after every print statement without
> > explicitly telling it to.
> >
> > Also, how can I flush the string in an input statement to stdout
> > before the input is provided by the user?  For example, here is a
> > code snippet:
> >
> > print '------------------------------'
> > sys.stdout.flush()
> > print '      Type of DRC run.'
> > sys.stdout.flush()
> > print '------------------------------'
> > sys.stdout.flush()
> > print '1) DRC (default)'
> > sys.stdout.flush()
> > print '2) Antenna DRC check'
> > sys.stdout.flush()
> > print 'q) quit'
> > sys.stdout.flush()
> >
> > while 1:
> >     selection = raw_input('Enter type of DRC to run from menu --> ')
> >     sys.stdout.flush()
> >
> > This prints the following to stdout:
> >
> > ------------------------------
> >       Type of DRC run.
> > ------------------------------
> > 1) DRC (default)
> > 2) Antenna DRC check
> > q) quit
> >
> > Unfortunately, it does not print the string in the raw_input statement
> > until the user has selected from the menu.  Thus the output looks like
> > this:
> >
> > 1
> > Enter type of DRC to run from menu -->
> >
> > I want the string printed to stdout before the user has to make a
> > selection.  Is there a way to do this?
> >
> > Thanks,
> > Aaron
> >
> > --
> > Aaron J. Ginn                     Motorola SPS
> > Phone: (480) 814-4463             SemiCustom Solutions
> > Fax:   (480) 814-4058             1300 N. Alma School Rd.
> > mailto:aaron.ginn at motorola.com    Chandler, AZ 85226
> 
> --
> http://www.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list