【发布时间】:2019-06-13 10:35:40
【问题描述】:
此代码在 Windows 和 Linux 之间打印不同的字符串。
test.py:
print(";".join([str(i) for i in range(10000)]))
平台:x86_64 Linux 4.4 .0-17763 - 微软
Python版本:3.7.2
终端:bash、fish
缩写输出:
$ python --version
Python 3.7.2
$ python test.py
0;1;2;3;4;5;6....9997;9998;9999
$ python -u test.py
0;1;2;3;4;5;6....9997;9998;9999
平台:Windows 10 1809
Python 版本:3.6.8、3.7.0、3.7.2
终端:cmd、powershell
缩写输出:
./python --version
Python 3.6.8
./python test.py
0;1;2;3;4;5;6....9997;9998;9999
./python -u test.py
0;1;2;3;4;5;6....2663;2664;2665;26
./python --version
Python 3.7.0
./python test.py
0;1;2;3;4;5;6....9997;9998;9999
./python -u test.py
0;1;2;3;4;5;6....2663;2664;2665;26
./python --version
Python 3.7.2
./python test.py
0;1;2;3;4;5;6....9997;9998;9999
./python -u test.py
0;1;2;3;4;5;6....2663;2664;2665;26
那么,为什么在 Windows 中,-u arg 会导致输出被截断(只是从 0 到 2666)?
(当使用python -u test.py > a.txt 将输出重定向到文件时,它可以正常工作。)
也许是关于缓冲的?
【问题讨论】:
-
我可以确认 - 这很奇怪。
-u确实forces the binary I/O layers of stdout/stderr to be unbuffered - stdin is always buffered; text I/O layer will be line-buffered..适用于您的输出 - 所以 maxline 大小似乎是 12222(这是您的输出的长度......) - linux 似乎在这里有更大的利润...... -
感谢您的回复。我对此还有一些问题。
maxline是什么意思? Windows 的常量?stdout在没有缓冲区的情况下实际上做了什么?我在哪里可以找到有关此的更多信息? -
maxline 正是我编造的,以表明 Windows 下的控制台似乎(在 -u 中)停止在 12222 个字符处写入标准输出 - 尽管我正在寻找我无法确认任何“硬“ 限制 - 谷歌没有帮助。也许一些 Windows-Crack 可以阐明这一点......
-
好的,谢谢您的回复。
-
@eryksun 感谢您回答 OhYee 的发现 - 如果您想让它成为真正的答案,请联系我并投票。希望它很快就会成为一个过时的历史事实......谁再使用win7(除了一些陷入中年的客户);D
标签: python python-3.x windows powershell