Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更改同一CLI行上的文本_Python_Curl_Command Line Interface - Fatal编程技术网

Python 更改同一CLI行上的文本

Python 更改同一CLI行上的文本,python,curl,command-line-interface,Python,Curl,Command Line Interface,例如,一些命令行实用程序能够在不生成新行的情况下更改行上的文本(如curl)。在Python中如何实现这一点?您可以使用带有“\r”(将光标放回行的开头)的sys.stdout.write 演示: 您必须确保print语句以回车结束,而不是换行符结束。对于Python2.6+,您可以使用这个非常愚蠢的示例来查看 from __future__ import print_function import time import sys print(".", end="\r") sys.stdout

例如,一些命令行实用程序能够在不生成新行的情况下更改行上的文本(如curl)。在Python中如何实现这一点?

您可以使用带有“\r”(将光标放回行的开头)的
sys.stdout.write

演示:


您必须确保print语句以回车结束,而不是换行符结束。对于Python2.6+,您可以使用这个非常愚蠢的示例来查看

from __future__ import print_function
import time
import sys

print(".", end="\r")
sys.stdout.flush()
time.sleep(1)
print("..", end="\r")
sys.stdout.flush()
time.sleep(1)
print("...")
sys.stdout.flush()
注意:当强制打印语句结束时,需要刷新标准输出以强制显示

from __future__ import print_function
import time
import sys

print(".", end="\r")
sys.stdout.flush()
time.sleep(1)
print("..", end="\r")
sys.stdout.flush()
time.sleep(1)
print("...")
sys.stdout.flush()