sys.stdout and Python3

Frank Millman frank at chagford.com
Sat Nov 23 08:26:04 EST 2013


Hi all

I have a question arising from another thread, but on a different topic, 
hence the new thread.

Under Python2, if you want to print a series of dots to the screen without a 
newline, you can do the following:

for i in range(10):
  sys.stdout.write('.')
  sys.stdout.flush()
  time.sleep(1)
sys.stdout.write('\n')

I tried it under Python3, and found that it differs in two ways -

1. Each 'write' is terminated by a newline
2. Each 'write' appends the length of the string written.

So instead of  -
    ..........
I get -
    .1
    .1
    etc

I found that the new 'print' function has a 'flush' argument, so it can now 
be written as -

for i in range(10):
  print('.', end='', flush=False)
  time.sleep(1)
print()

I tried to read the docs on sys.stdout, and I see a lot of changes in this 
area, but I could not see the reason for the above behaviour.

Is there a way to reproduce the Python2 behaviour in Python3 using 
sys.stdout?

Thanks

Frank Millman






More information about the Python-list mailing list