sys.stdout and Python3

F

Frank Millman

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top