我有一个需要很长时间才能运行的 Python 脚本。我非常希望命令行输出有一点“等待”动画,就像我们在浏览器中为 AJAX 请求获得的漩涡状圆圈一样。类似于 '\' 的输出,然后将其替换为 '|',然后是 '/',然后是 '-'、'|' 等,就像文本在循环中一样。我不确定如何用 Python 替换以前打印的文本。
最佳答案
使用 \r
和 print-without-newline(即带逗号的后缀):
animation = "|/-\\"
idx = 0
while thing_not_complete():
print(animation[idx % len(animation)], end="\r")
idx += 1
time.sleep(0.1)
对于 Python 2,使用此 print
语法:
print animation[idx % len(animation)] + "\r",
关于python - 命令提示符中的“等待”动画 (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7039114/