2016-12-29 98 views
0
print(7) 
sleep(10) 
print(8) 

當我在Spyder的命令行中輸入了這個信息時,我期望7被打印,然後在10秒後打印8,但是,發生的事情是7和8都是爲什麼會出現這種情況呢?time.sleep in python exceution order

我需要這個功能的原因是我希望Python繼續搜索目錄中的文件,直到它最終出現。

done = os.path.exists ("done.txt") 
while not done: 
    sleep(10) 
    done = os.path.exists ("done.txt") 

這項工作?

編輯:糾正存在爲存在。

+4

可能重複的[Python3睡眠()問題](http://stackoverflow.com/questions/4456581/python3-sleep-problem) – Blorgbeard

+0

對於你的問題不會'一次'初始化一次?那麼「睡眠(10)」將不會發生。也許你應該檢查'os.path.exist()'作爲'while'條件。像'while os.path.exists('/ your/path/file.txt')'? –

+0

它應該是'os.path.exists'而不是'os.path.exist'。除此之外,這應該工作得很好。 –

回答

2

第一:它應該是os.path.exists而不是os.path.exist

除此之外,這應該工作得很好。

二:我覺​​得kiran.konduru誤解了這個問題,但你肯定可以簡化這一點,並刪除了done變量的需要:

while not os.path.exists("done.txt"): 
    sleep(10) 

最後:本time.sleep()功能得到緩衝的Spyder的命令行,見here。如果您使用sys.stdout.write()sys.stdout.flush()而不是簡單的print,那麼在該命令行中也可以正常工作。

+0

非常感謝。 :) – Lost1