Python 退格故障
为什么我的Python时钟只从Python2运行,而Python3什么都不做Python 退格故障,python,python-3.x,console-application,Python,Python 3.x,Console Application,为什么我的Python时钟只从Python2运行,而Python3什么都不做 from __future__ import print_function import time wipe = '\b'*len(time.asctime()) print("The current date and time are: "+' '*len(wipe), end='') while True: print(wipe+time.asctime(), end='') time.sleep(
from __future__ import print_function
import time
wipe = '\b'*len(time.asctime())
print("The current date and time are: "+' '*len(wipe), end='')
while True:
print(wipe+time.asctime(), end='')
time.sleep(1)
在Python3中,需要刷新打印缓冲区以强制将字符写入屏幕 加 转到脚本的开头,并将循环更改为
while True:
print(wipe+time.asctime(), end='')
sys.stdout.flush()
time.sleep(1)
在Python3中,需要刷新打印缓冲区以强制将字符写入屏幕 加 转到脚本的开头,并将循环更改为
while True:
print(wipe+time.asctime(), end='')
sys.stdout.flush()
time.sleep(1)
问题不在于python版本,而在于您忘记刷新标准输出。尝试将代码更改为:
from __future__ import print_function
import time
import sys
wipe = '\b'*len(time.asctime())
print("The current date and time are: "+' '*len(wipe), end='')
while True:
print(wipe+time.asctime(), end='')
sys.stdout.flush()
time.sleep(1)
sys.stdout仅在打印换行符时刷新。问题不在于python版本,而在于您忘记刷新标准输出。尝试将代码更改为:
from __future__ import print_function
import time
import sys
wipe = '\b'*len(time.asctime())
print("The current date and time are: "+' '*len(wipe), end='')
while True:
print(wipe+time.asctime(), end='')
sys.stdout.flush()
time.sleep(1)
sys.stdout仅在打印换行符时才会刷新。实际上,这对于Python 2.x也是必要的(至少在使用打印函数时是如此)。在我的系统上,在Python 2中没有它也无法工作。在我的系统(Windows 7 x64)上,在Python 2.7中没有它也可以工作。实际上,这对于Python 2.x也是必要的(至少在使用打印功能时是这样)。在我的系统上,Python 2中没有这个功能也不行。在我的系统(Windows 7 x64)上,Python 2.7中没有这个功能也行。