我有一个Python程序,通过我定义的nav(a, b)函数导航到一个网站,在该网站上我将下载一些pyfits数据以供另一个脚本使用。该网站为我拥有的每组(a,b)在目录中提供不同的pyfits文件。
我想知道是否可以使用for
循环遍历这个目录,并且每次使用nav(a, b)函数时,告诉Python在我下载文件时暂停,然后在我要求它继续时恢复。我之前在IDL中做过类似的事情,但不知道如何在Python中实现。
否则,我想我只能运行该程序200次,每次更换(a,b)的值,这将花费太多时间。
raw_input("Press Enter to continue...")
Python 3:
input("Press Enter to continue...")
如果您可以在Python代码中下载文件,请这样做,而不是为每个文件手动执行任务。
import time
time.sleep(1.3) # Seconds
示例:
import time
print "Start Time: %s" % time.ctime()
time.sleep(5)
print "End Time: %s" % time.ctime()
Start Time: Tue Feb 17 10:19:18 2009
End Time: Tue Feb 17 10:19:23 2009
使用 while
循环,等待您的下载完成:
for ... :
nav(a,b)
while downloading_not_finished:
time.sleep(X)
因此,在每个X周期内测试一次条件,并且在下载部分完成之前会再次测试条件。
好的,这里有两种在Python中暂停程序的方法。
You can use the input function.
# Python 2
raw_input("Downloading....")
# Python 3
input("Downloading....")
当用户按下 Enter 键时,程序将暂停执行等待。
You can use the time.sleep() function.
import time
time.sleep(number of seconds)
关于Python shell的设计:
import sys
from time import sleep
try:
shell = sys.stdout.shell
except:
print('Run It In Shell')
dots = '........';
shell.write('Downloading')
sleep(0.5)
for dot in dots:
shell.write(dot)
sleep(0.1)
shell.write('\n')
sleep(0.4)
shell.write('Saving Files')
sleep(0.5)
for doot in dots:
shell.write(dot)
sleep(0.1)
shell.write('\n')
sleep(0.4)
对于在控制台中使用Python:
from time import sleep
print('Downloading')
sleep(1)
print('Saving Files')
sleep(1)
while True:
#do something
asleep = True
while (asleep):
if cv2.waitKey(1) == 32: #Wait for space key
asleep = False
nav
代码。您是使用urllib2
或类似工具进行下载吗? - Jamie Cockburnnav
之前列出目录,然后让 Python 轮询该目录等待新文件的出现,然后可能要等待文件大小停止变化或其他度量标准来判断“文件已下载”。 - Ben