无法截断 sys.stdout; returns "io.UnsupportedOperation: truncate"
Cannot truncate sys.stdout; returns "io.UnsupportedOperation: truncate"
完整代码:
import sys
import time
counter = 0
while True:
sys.stdout.write(str(counter))
time.sleep(1)
sys.stdout.truncate()
counter += 1
重要的部分
sys.stdout.truncate()
问题(S)
为什么 sys.stdout.truncate()
return 出错了?如果 sys.stdout.truncate()
不起作用,我如何截断 sys.stdout
?
操作系统和更多信息
操作系统:Windows
操作系统版本:Windows10
编程语言:Python
编程语言版本:Python3.6
其他详细信息:运行 来自命令行
sys.stdout 是一个文件对象,它对应于解释器的标准输出并且没有 truncate() 方法:
https://docs.python.org/3/library/sys.html#sys.stdout
您似乎想要创建状态栏样式的输出。
这应该适用于 Python3:
import time
counter = 0
while True:
print("{}".format(counter), end="\r")
time.sleep(1)
counter += 1
有关详细信息,请参阅 How to overwrite the previous print to stdout in python?
完整代码:
import sys
import time
counter = 0
while True:
sys.stdout.write(str(counter))
time.sleep(1)
sys.stdout.truncate()
counter += 1
重要的部分
sys.stdout.truncate()
问题(S)
为什么 sys.stdout.truncate()
return 出错了?如果 sys.stdout.truncate()
不起作用,我如何截断 sys.stdout
?
操作系统和更多信息
操作系统:Windows
操作系统版本:Windows10
编程语言:Python
编程语言版本:Python3.6
其他详细信息:运行 来自命令行
sys.stdout 是一个文件对象,它对应于解释器的标准输出并且没有 truncate() 方法:
https://docs.python.org/3/library/sys.html#sys.stdout
您似乎想要创建状态栏样式的输出。 这应该适用于 Python3:
import time
counter = 0
while True:
print("{}".format(counter), end="\r")
time.sleep(1)
counter += 1
有关详细信息,请参阅 How to overwrite the previous print to stdout in python?