[Tutor] "Print" behaviour inside a loop?
chinook.nr at tds.net
Sun Jun 12 07:16:00 CEST 2005
On Sat, 11 Jun 2005 19:42:27 -0400, Simon Gerber wrote
(in article <42AB76E3.90107 at gmail.com>):
> Firstly, thanks to everyone who helped me find my own IP address. That
> was a big help. That bit is working now, and working very nicely. I am
> now stuck on something purely aesthetic - printing a few dots across the
> screen to provide a bit of feedback while the VPN tunnel is being
> def vpn_connect(choice):
> import time
> ip_addr = ""
> tries = 0
> retries = 10
> print "Connecting to %s" % (choice),
> os.system("pon %s" % (choice))
> while ip_addr == "" and tries < retries:
> print "...", # This is the line causing problems
> ip_addr = get_addr()
> if ip_addr != '':
> #create the route!
> tries += 1
> It works. The problem is, nothing is displayed on screen until after the
> connection occurs - at which point we see:
> "Connecting to Prodigi ... ... ... ... ... ... ... "
> If I remove the comma at the end of the marked line, the ellipses print
> every .5 seconds as desired, except they print down the screen of course!
> After googling around a little, I found a post that seemed to say
> Python won't draw the results of 'print' statements until it hits a
> newline. I tried using sys.stout.write('...'), but had the same problem
> there, too. I've also found a few progress bar classes around the web,
> but I not exactly want a progress bar. I just want to print '...' every
> few seconds until connected.
> Any hints? I looked up the python reference manual, but couldn't find
> any way to force print statements to draw. Should I be looking into
> threads, perhaps?
> Tutor maillist - Tutor at python.org
Just use sys.stdout.writelines(' ... ')
>>> import sys
>>> sys.stdout.writelines ('... '); sys.stdout.writelines ('... ')
... ... >>>
That what your looking for?
More information about the Tutor