2010-11-27 3 views
0

내 파이썬 시계가 파이썬 2에서만 실행되는 이유는 무엇입니까?백 스페이스 문제

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) 

답변

4

파이썬 3에서는 인쇄 버퍼를 플러시하여 화면에 글자를 쓰도록해야합니다.

스크립트의 시작

import sys 

를 추가하고

while True: 
    print(wipe+time.asctime(), end='') 
    sys.stdout.flush() 
    time.sleep(1) 
+0

사실, 이것은 또한 Python 2.x에서 필요합니다 (최소한 print 함수를 사용할 때). –

+0

내 시스템에서는 Python 2에서도이 기능이 작동하지 않습니다. –

+0

내 시스템 (Windows 7 x64)에서는 Python 2.7에서이 기능이 작동하지 않습니다. –

1

문제는 파이썬 버전되지 않습니다에 루프를 변경할 수 있지만, 당신은 표준 출력을 플러시하는 것을 잊었다 않고있다. 코드를 다음으로 변경해보십시오.

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은 줄 바꿈이 인쇄 될 때만 플래시합니다.