在Python中,清空输出内容可以通过以下几种方法实现:
1. 使用`end`参数:
print("Hello, world!", end="")
2. 使用特殊字符`\r`:
print("Loading", end="\r")time.sleep(1)print("\rLoading...", end="")
3. 使用`os`模块调用系统命令`cls`(Windows)或`clear`(Unix/Linux):
import osdef clear_screen():system_name = platform.system()if system_name == "Windows":os.system("cls")elif system_name in ["Linux", "Darwin"]:os.system("clear")else:print("\n" * 100)
4. 使用`file.truncate(0)`清空文件内容:
with open("test.txt", "w") as file:file.truncate(0)
5. 使用`print("\n" * 100)`输出一定数量的空行来清空控制台窗口内容:
print("\n" * 100)

