使用 Python urllib2 下载时忽略丢失的文件
Ignore missing file while downloading with Python urllib2
问题: 正如标题所述,我正在根据年份和日期通过 ftp 从 NOAA 下载数据。我已将我的脚本配置为经过一系列年并下载每一天的数据。但是,在没有文件存在的日子里,脚本会挂起。发生的事情是它只是不断地重新加载同一行,说该文件不存在。如果没有 time.sleep(5),脚本会疯狂地打印到日志中。
解决方案: 以某种方式跳过丢失的一天并转到下一天。我已经探索了 continue(也许我把它放在了错误的位置),制作了一个空目录(不优雅,仍然不会超过丢失的一天)。我很茫然,我忽略了什么?
脚本如下:
##Working 24km
import urllib2
import time
import os
import os.path
flink = 'ftp://sidads.colorado.edu/DATASETS/NOAA/G02156/24km/{year}/ims{year}{day}_24km_v1.1.asc.gz'
days = [str(d).zfill(3) for d in range(1,365,1)]
years = range(1998,1999)
flinks = [flink.format(year=year,day=day) for year in years for day in days]
from urllib2 import Request, urlopen, URLError
for fname in flinks:
dl = False
while dl == False:
try:
# req = urllib2.Request(fname)
req = urllib2.urlopen(fname)
with open('/Users/username/Desktop/scripts_hpc/scratch/'+fname.split('/')[-1], 'w') as dfile:
dfile.write(req.read())
print 'file downloaded'
dl = True
except URLError, e:
#print 'sleeping'
print e.reason
#print req.info()
print 'skipping day: ', fname.split('/')[-1],' was not processed for ims'
continue
'''
if not os.path.isfile(fname):
f = open('/Users/username/Desktop/scripts_hpc/empty/'+fname.split('/')[-1], 'w')
print 'day was skipped'
'''
time.sleep(5)
else:
break
#everything is fine
研究:我浏览了其他问题,它们很接近,但似乎没有一针见血。 ,how to skip over a lines of a file if they aempty 任何帮助将不胜感激!
谢谢!
我想当你站起来走开去喝杯咖啡时,事情就会变得清晰起来。显然我的 while 语句中有什么东西被挂断了(仍然不确定为什么)。当我把它拿出来并添加 pass 而不是 continue 它表现正确。
这是现在的样子:
for fname in flinks:
try:
req = urllib2.urlopen(fname)
with open('/Users/username/Desktop/scripts_hpc/scratch/'+fname.split('/')[-1], 'w') as dfile:
dfile.write(req.read())
print 'file downloaded'
except URLError, e:
print e.reason
print 'skipping day: ', fname.split('/')[-1],' was not processed for ims'
pass
time.sleep(5)
在 except
上,使用 pass
而不是 continue
,因为它只能在循环内部使用(for
、while
)。
有了它,您就不需要处理丢失的文件,因为 Python 会忽略错误并继续。
问题: 正如标题所述,我正在根据年份和日期通过 ftp 从 NOAA 下载数据。我已将我的脚本配置为经过一系列年并下载每一天的数据。但是,在没有文件存在的日子里,脚本会挂起。发生的事情是它只是不断地重新加载同一行,说该文件不存在。如果没有 time.sleep(5),脚本会疯狂地打印到日志中。
解决方案: 以某种方式跳过丢失的一天并转到下一天。我已经探索了 continue(也许我把它放在了错误的位置),制作了一个空目录(不优雅,仍然不会超过丢失的一天)。我很茫然,我忽略了什么?
脚本如下:
##Working 24km
import urllib2
import time
import os
import os.path
flink = 'ftp://sidads.colorado.edu/DATASETS/NOAA/G02156/24km/{year}/ims{year}{day}_24km_v1.1.asc.gz'
days = [str(d).zfill(3) for d in range(1,365,1)]
years = range(1998,1999)
flinks = [flink.format(year=year,day=day) for year in years for day in days]
from urllib2 import Request, urlopen, URLError
for fname in flinks:
dl = False
while dl == False:
try:
# req = urllib2.Request(fname)
req = urllib2.urlopen(fname)
with open('/Users/username/Desktop/scripts_hpc/scratch/'+fname.split('/')[-1], 'w') as dfile:
dfile.write(req.read())
print 'file downloaded'
dl = True
except URLError, e:
#print 'sleeping'
print e.reason
#print req.info()
print 'skipping day: ', fname.split('/')[-1],' was not processed for ims'
continue
'''
if not os.path.isfile(fname):
f = open('/Users/username/Desktop/scripts_hpc/empty/'+fname.split('/')[-1], 'w')
print 'day was skipped'
'''
time.sleep(5)
else:
break
#everything is fine
研究:我浏览了其他问题,它们很接近,但似乎没有一针见血。
谢谢!
我想当你站起来走开去喝杯咖啡时,事情就会变得清晰起来。显然我的 while 语句中有什么东西被挂断了(仍然不确定为什么)。当我把它拿出来并添加 pass 而不是 continue 它表现正确。
这是现在的样子:
for fname in flinks:
try:
req = urllib2.urlopen(fname)
with open('/Users/username/Desktop/scripts_hpc/scratch/'+fname.split('/')[-1], 'w') as dfile:
dfile.write(req.read())
print 'file downloaded'
except URLError, e:
print e.reason
print 'skipping day: ', fname.split('/')[-1],' was not processed for ims'
pass
time.sleep(5)
在 except
上,使用 pass
而不是 continue
,因为它只能在循环内部使用(for
、while
)。
有了它,您就不需要处理丢失的文件,因为 Python 会忽略错误并继续。