Python Ftp 不是目录错误

Python Ftp Not a Direcoty error

我正在尝试从 ftp 服务器下载文件并将数据导入到 Django。所以我创建了一个列表,其中包含服务器地址、登录详细信息、路径、文件名以及要下载的文件的路径,并将其传递给执行下载的函数。当将它移动到客户端服务器时它正在我的系统中工作文件它显示错误如

" 错误下载 C_VAR1_31012014_1.DAT - [Errno 20] 不是目录:'common/VARRate/C_VAR1_31012014_1.DAT"

这是列表的样子

self.fileDetails = { 'NSE FO VAR RATE FILE': ('ftp.xxx.com', username, passwd, 'common/VARRate', 'C_VAR1_\d{4}201[45]_\d{1}.DAT', 'Data/samba/Ftp/Capex10/NSECM/VAR RATE'), }

  for fileType in self.fileDetails:
        self.ftpDownloadFiles(fileType)

此详细信息将传递给函数后的函数

def ftpDownloadFiles(self, fileType):
    logging.info('Started ' + str(fileType))
    try:
        ftpclient = ftplib.FTP(self.fileDetails[fileType][FDTL_SRV_POS],
                                self.fileDetails[fileType][FDTL_USR_POS],
                                self.fileDetails[fileType][FDTL_PSWD_POS],
                                timeout=120)
        #ftpclient.set_debuglevel(2)
        ftpclient.set_pasv(True)
        logging.info('Logged in to ' + self.fileDetails[fileType][FDTL_SRV_POS] +\
                          time.asctime())
        logging.info('\tfor type: '+ fileType)
    except BaseException as e:
        print e
        return

    remotepath = self.fileDetails[fileType][FDTL_PATH_POS]

    #matched, unmatched, downloaded = 0
    try:
        ftpclient.cwd(remotepath)
        ftpclient.dir(filetimestamps.append)
    except BaseException as e:
        logging.info('\tchange dir error : ' + remotepath + ' ' +\
                         e.__str__())
    self.walkTree(ftpclient, remotepath, fileType)
    #logging.info('\n\tMatched %d, Unmatched %d, Downloaded %d' 
    # % (matched, unmatched, downloaded))
    ftpclient.close()

从这里调用下一个函数,下载过程将开始

def walkTree(self, ftpclient, remotepath, fileType):
    # process files inside remotepath; cwd already done
    # remotepath to be created if it doesnt exist locally
    copied=matched=downloaded=imported = 0
    files = ftpclient.nlst()

    localpath = self.fileDetails[fileType][FDTL_DSTPATH_POS]
    rexpCompiled = re.compile(self.fileDetails[fileType][FDTL_PATRN_POS])

    for eachFile in files:
        try:
            ftpclient.cwd(remotepath+'/'+eachFile)
            self.walkTree(ftpclient, remotepath+'/'+eachFile+'/', fileType)
        except ftplib.error_perm: # not a folder, process the file
            # every file to be saved in same local folder as on ftp srv
            saveFolder = remotepath
            saveTo = remotepath + '/' + eachFile
            if not os.path.exists(saveFolder):
                try:
                    os.makedirs(saveFolder)
                    print "directory created"
                except BaseException as e:
                    logging.info('\tcreating %s : %s' % (saveFolder, e.__str__()))
            if (not os.path.exists(saveTo)):
                try:
                    ftpclient.retrbinary('RETR ' + eachFile, open(saveTo, 'wb').write)
                    #logging.info('\tdownloaded '  + saveTo)
                    downloaded += 1
                except BaseException as e:
                    logging.info('\terror downloading %s - %s' % (eachFile, e.__str__()))
                except ftplib.error_perm:
                    logging.info('\terror downloading %s - %s' % (eachFile, ftplib.error_perm))  
            elif (fileType == 'NSE CASH CLOSING FILE'):  # spl case if file exists
                try:
                    # rename file
                    yr = int(time.strftime('%Y')) - 1
                    os.rename(saveTo, saveTo + str(yr))
                    # download it
                    ftpclient.retrbinary('RETR ' + eachFile, open(saveTo, 'wb').write)
                    downloaded += 1
                except BaseException as e:
                    logging.info('\terror rename/ download %s - %s' % (eachFile, e.__str__()))

谁能帮我解决这个问题

尝试使用 os.path.join() 而不是硬编码的斜线作为 os 下载的路径分隔符。 / 或 \ 取决于本地 os。

例如在您的代码中:

saveTo = remotepath + '/' + eachFile

会变成:

saveTo = os.path.join(remotepath,eachFile)

https://docs.python.org/2/library/os.path.html