Python 如何在命令行上清理特定对象
我想知道是否可以在Python脚本运行的命令shell上清除特定的内容 例如,脚本包含:Python 如何在命令行上清理特定对象,python,python-3.x,command-line,cmd,Python,Python 3.x,Command Line,Cmd,我想知道是否可以在Python脚本运行的命令shell上清除特定的内容 例如,脚本包含: print ("Hello world") 当我双击这个脚本时,会弹出cmd,我会在命令行上看到helloworld。现在我想知道,当cmd运行时,是否可以从shell中清除Hello World,并在那里编写其他内容?比如: print ("Hello world") time.sleep(1) # a module replace New Text with Hello world print("N
print ("Hello world")
当我双击这个脚本时,会弹出cmd,我会在命令行上看到helloworld
。现在我想知道,当cmd运行时,是否可以从shell中清除Hello World
,并在那里编写其他内容?比如:
print ("Hello world")
time.sleep(1)
# a module replace New Text with Hello world
print("New Text")
您可以为此使用ANSI转义字符:
sys.stdout.write("\033[F") # Cursor up to line
sys.stdout.write("\033[K") # Clear line
或者,虽然这在Windows cmd中有时不起作用,但请尝试以下操作:
sys.stdout.write("\r") # Move to line beginning
或者根据您的需要:
import time
print("Hello world", end="\r")
time.sleep(1)
print("New text ") # Trailing slashes to completely override "Hello world"
您可以为此使用ANSI转义字符:
sys.stdout.write("\033[F") # Cursor up to line
sys.stdout.write("\033[K") # Clear line
或者,虽然这在Windows cmd中有时不起作用,但请尝试以下操作:
sys.stdout.write("\r") # Move to line beginning
或者根据您的需要:
import time
print("Hello world", end="\r")
time.sleep(1)
print("New text ") # Trailing slashes to completely override "Hello world"
刷新打印的文本,不要在末尾换行,而是写一个
\r
要在下一行的开头开始打印,请执行以下操作:
from __future__ import print_function # to make it work with Python 2, just in case
import time
print ("Hello world", end='\r', flush=True)
time.sleep(1)
print ("New Text ", end='\r', flush=True)
time.sleep(1)
print ("Last Text ")
刷新打印的文本,不要在末尾换行,而是写一个
\r
要在下一行的开头开始打印,请执行以下操作:
from __future__ import print_function # to make it work with Python 2, just in case
import time
print ("Hello world", end='\r', flush=True)
time.sleep(1)
print ("New Text ", end='\r', flush=True)
time.sleep(1)
print ("Last Text ")
我认为cmd中有一些特殊字符可以移动光标并删除特定部分A模块用Hello world替换新文本签出curses模块。我认为cmd中有一些特殊字符可以移动光标并删除特定部分A模块用Hello world替换新文本签出curses模块。这就是我所做的我想要,这就是我想要的。