[英]sys.stdout.write not working as expected in Python
I have two functions prints()
and clear()
. 我有两个函数
prints()
和clear()
。 The function prints()
print lines of text and the function clear()
delete the printed lines with this method: 函数
prints()
打印文本行,函数clear()
使用以下方法删除打印行:
def clear():
i = 0
while True:
if(i > globals['l']):
break
else:
sys.stdout.write("\033[F \033[K")
i += 1
where globals['l']
is the number of lines to clear. 其中
globals['l']
是要清除的行数。
Then after the function clear()
runs and the lines are cleared, the function prints()
run again etc... 然后在函数
clear()
运行并且行被清除之后,函数prints()
再次运行,等等。
I don't understand why the function clear()
is clearing only 22 lines of 32 lines. 我不明白为什么函数
clear()
仅清除32行中的22行。 But if I have, for example, 19 lines it is working perfectly. 但是,例如,如果我有19条线,则它工作正常。 Where is the problem?
问题出在哪儿? How can I fix this?
我怎样才能解决这个问题?
Try this: 尝试这个:
def clear(line_count):
sys.stdout.write("\033[F \033[K" * line_count)
sys.stdout.flush()
EDIT: Works only on Unix systems 编辑:仅适用于Unix系统
You can use: 您可以使用:
print("\033c")
Or: 要么:
import subprocess
subprocess.call(["printf", "\033c"])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.