print(7)
sleep(10)
print(8)
当我在Spyder的命令行中键入此内容时,我预计会打印7,然后在10秒后打印8,但是,发生的事情是在10秒暂停后打印7和8 ,为什么会这样呢?
我需要这个功能的原因是我希望Python继续搜索目录中的文件,直到它最终出现。
done = os.path.exists ("done.txt")
while not done:
sleep(10)
done = os.path.exists ("done.txt")
这会有用吗?
编辑:更正存在。
答案 0 :(得分: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
,那么在该命令行中也能正常运行。