2010-05-11 33 views
2

麻煩當運行,例如:出現經與time.sleep

print("[",end=" ") 
time.sleep(1) 
print("=",end=" ") 
time.sleep(1) 
print("=",end=" ") 
time.sleep(1) 
print("=",end=" ") 
time.sleep(1) 
print("=",end=" ") 
time.sleep(1) 
print("=",end=" ") 
time.sleep(1) 
print("=",end=" ") 
time.sleep(1) 
print("=",end=" ") 
time.sleep(1) 
print("=",end=" ") 
time.sleep(1) 
print("=",end=" ") 
time.sleep(1) 
print("=",end=" ") 
time.sleep(1) 
print("]",end=" ") 

什麼也沒有發生10秒,那麼整個[= = = = = = = = = =]。我怎樣才能防止這樣做,以便它可以作爲一種進度條?

+7

oww,這是討厭的。什麼是循環?他們從來沒有做過任何不好的事情,應該得到如此公然的無知!有一顆心,男人! – LukeN 2010-05-11 22:42:15

+0

[與函數中的time.sleep()sys.stdout.write()問題]可能重複(http://stackoverflow.com/questions/2808832/problems-with-sys-stdout-write-with-time - 功能中的睡眠) – tzot 2010-06-07 14:03:07

回答

3

其實,一個進度條屬於sys.stderr,這是(非常方便,而不是巧合的話)不進行緩衝。所以我建議你:

print("=", end=" ", file=sys.stderr) 

改爲。

PS在POSIX兼容的操作系統中的標準輸入,輸出和錯誤流的大綱可以在Wikipedia中找到:Standard streams。簡而言之,stdin是進程的輸入;標準輸出是一個過程的有用輸出,結果; stderr用於警告,錯誤和帶外(例如進度條)輸出。

+0

這會做什麼?我不明白,我只是一個初學者。它仍然會輸出到外殼? – Kudu 2010-05-12 22:26:14

+0

@Waterfox:是的,它仍然會輸出到「終端」。繼續嘗試。 – tzot 2010-05-12 23:57:18

+0

我不明白,有什麼區別? – Kudu 2010-05-13 19:20:38

0

你需要你想寫更新每次使用sys.stdout.flush()刷新stdout

5

嘗試在每次打印後沖水標準輸出:

import sys 

print("=",end=" ") 
sys.stdout.flush()