2013-10-08 6 views
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 당신이 입력을 요청하시기 바랍니다 입력 날짜 코드를 실행하면 이것은 파이썬 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) 

답변

-1

: break 휴식을 다운로드 루프에서 바로 첫 번째 파일을 다운로드 같이

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() 

그냥 위의 continue에 대한 break를 교체하고 그것을 작동합니다. - 더 나은 점은 for 루프에 다른 명령문이 없으므로 해당 행을 완전히 제거하는 것입니다.

+0

'계속'을 시도했지만 작동하지 않습니다. '휴식'은 수업 시간입니다. 필요한 이름의 파일이 발견되면 작업을 중단하는 데이 파일을 사용했습니다. 그런 다음 다른 클래스 인스턴스가 처음 게시 된 코드의 맨 아래에있는 루프에서 시작됩니다. 따라서 '휴식'은 수업 시간 내에 있습니다. 우리가 말하는 루프는 클래스 외부에 있으며 클래스 인스턴스를 반복합니다. – multigoodverse