Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

python - Output from sys.stdout in interactive mode

I tested sys.stdout.write in interactive mode; why do I get the 'extra' 1 and 2 suffixed to the numbers? If I run the code from a file I get the expected output (1234...) Python 3.3 on a Windows machine

>>> import sys
>>> for i in range(15):
...     sys.stdout.write(str(i))
...
01
11
21
31
41
51
61
71
81
91
102
112
122
132
142
>>>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Python is also echoing the return value of sys.stdout.write() call, which is the number of bytes written:

>>> import sys
>>> written = sys.stdout.write('10')
10>>> written
2

Here the next prompt follows the '10' written without a newline.

Or, as a different way of demoing, writting 0 bytes in a loop prints 0 that many times:

>>> for i in range(3):
...     sys.stdout.write('')
... 
0
0
0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...