如何修复Windows/Mac OS上的“\r”功能

2024-10-06 09:48:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个问题,我想摆脱一些打印,但它被完全忽略,有几行打印出来。对于这些解决方案,我总是使用\r。有办法扭转这种局面吗?(我现在正在使用Mac操作系统,但保证很快就会切换到Windows。)@Feodoran告诉我\r只适用于Linux计算机,这是一个问题。这方面的代码是:

print ('virus_prevention.fix.virus.|attempt_enter')
time.sleep(2)
for i in range(1):
  sys.stdout.write(str(i)+'\r')
print ('virus_prevention.fix.virus|.attempt_enter')
time.sleep(0.1)
for i in range(1):
sys.stdout.write(str(i)+'\r')

输出如下:

virus_prevention.fix.virus.|attempt_enter
0
virus_prevention.fix.virus|.attempt_enter
0

我试过sys.stdout.write而不是print,但似乎什么都没有改变。我不知道在这种情况下该怎么办,但如果有人能给我一些建议,请告诉我。它正在删除完全不起作用的打印行


Tags: infortimestdoutsysrangesleepfix
1条回答
网友
1楼 · 发布于 2024-10-06 09:48:21
import time
import sys
sys.stdout.write('virus_prevention.fix.virus.|attempt_enter')
time.sleep(2)
for i in range(1):
    sys.stdout.write('\r')
    sys.stdout.flush()
    sys.stdout.write(str(i))
sys.stdout.write('virus_prevention.fix.virus|.attempt_enter')
time.sleep(2)
for i in range(1):
    sys.stdout.write('\r')
    sys.stdout.flush()
    sys.stdout.write(str(i))

这能解决你的问题吗

相关问题 更多 >