0
我已經寫了一個類,它在調用類實例時從ftp站點(ftp://ladsweb.nascom.nasa.gov/allData/5/MOD11A1/)下載一個文件名的部分文件。後來我寫了一個for循環,並在for循環中集成了類實例,以便下載多個日期範圍內的文件。這是一個日期範圍,因爲這些文件是根據它們生成的日期命名的。所以有每日文件。當你運行代碼時,你被要求輸入一系列的日期。範圍內的第一個文件下載成功,但程序停止並打印以下錯誤那麼當:只下載循環的第一個文件
Traceback (most recent call last):
File "ftplib04Simplified.py", line 42, in <module>
FtpDownloader("ladsweb.nascom.nasa.gov","/allData/5/MOD11A1/",a).findFile(10,11)
File "ftplib04Simplified.py", line 32, in findFile
self.ftp.retrbinary('RETR %s' % filename, open(filename,'wb').write)
File "/usr/lib/python3.3/ftplib.py", line 424, in retrbinary
with self.transfercmd(cmd, rest) as conn:
File "/usr/lib/python3.3/ftplib.py", line 386, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "/usr/lib/python3.3/ftplib.py", line 352, in ntransfercmd
resp = self.sendcmd(cmd)
File "/usr/lib/python3.3/ftplib.py", line 259, in sendcmd
return self.getresp()
File "/usr/lib/python3.3/ftplib.py", line 233, in getresp
raise error_perm(resp)
ftplib.error_perm: 550 No such file.
shell returned 1
我知道純一流的設計將讓我羞愧,但這是完整的代碼,我已經寫: PS這是用Python編寫3.如果您運行的代碼,當問及輸入,請輸入一個日期2001年後
import ftplib
import math
import datetime as dt
import time
class FtpDownloader:
"""Downloads raster tiles given the date, and tile row and column number"""
def __init__(self,site,directory,raw_date,ftp=None):
"""logs in to ftp"""
self.site=site
self.directory=directory
self.raw_date=raw_date
self.ftp=ftplib.FTP(site)
self.ftp.login()
self.convert_date()
def convert_date(self):
"""converts YYYY-MM-DD format to year and day of the year"""
year=self.raw_date.strftime("%Y")
day=self.raw_date.strftime("%j")
self.go_to_folder(year,day)
def go_to_folder(self,year,day):
"""sets the current FTP directory"""
self.ftp.cwd(self.directory+"%s/%s/" % (year,day))
def findFile(self,h,v,fileList=[]):
"""checks for the file containing the given h and h and downloads it using retrbinary"""
hh= "%02d" % h
vv= "%02d" % v
tilename = "h%sv%s" % (hh,vv)
print ("Image tile %s is downloading..." % tilename)
self.ftp.retrlines('NLST',fileList.append)
for filename in fileList:
if tilename in filename:
self.ftp.retrbinary('RETR %s' % filename, open(filename,'wb').write)
print ("File downloaded")
break
else:
print (filename, "not found")
self.ftp.close()
start=dt.datetime.strptime(input("Enter a start date in YYYY-MM-DD format "),"%Y-%m-%d")
end=dt.datetime.strptime(input("Enter an end date in YYYY-MM-DD format "),"%Y-%m-%d")
for i in range((end-start).days + 1):
a=(start+dt.timedelta(days=i)).date()
FtpDownloader("ladsweb.nascom.nasa.gov","/allData/5/MOD11A1/",a).findFile(10,11)
我嘗試'繼續',但它不起作用。 '休息'在課堂內。如果找到具有所需名稱的文件,我已經使用它來中斷操作。但是,然後,另一個類實例從我發佈的代碼底部的循環開始。所以,這個'休息'在課堂上。我們正在討論的循環是在類之外並遍歷類實例。 – multigoodverse