如何通过python3.3使用Filezilla

2024-10-01 11:35:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要下载大量数据(每天大约30GB,其中有些文件大于1GB)。特别是对于大文件,通过FileZilla下载的速度比手动下载(在Windows中单击鼠标右键并保存)或通过Python3.3下载要快一个数量级(几乎10倍)。在

我下面给出的python3.3脚本递归地遍历FTP站点上的目录,并且只有在该文件还没有下载的情况下才下载该文件(它是这个论坛上两个不同帖子的修改版本)。我想替换python3.3的本机ftplib(在下面的代码中)来使用FileZilla。在

import ftplib
import os

def traverseFTP(ftpObj,depth,cdir,bdir):
    # print(depth)
    if depth>10:
        return []
    else:
        # do the following
        data=[]
        ftpObj.dir(data.append)
        for entry in data:
            # dName=entry[55:len(entry)].strip()
            dName=entry.split()[-1]
            dIsDir=entry[0]
            if dName!="." and dName!="..":
                if dIsDir!="d":
                    # download if not already downloaded
                    dFullName=bdir+cdir+"\\"+dName
                    if not os.path.isfile(dFullName):
                        print("... Downloading ",dName)
                        if not os.path.isdir(bdir+cdir): os.makedirs(bdir+cdir)
                        ftpObj.retrbinary('RETR %s' % dName,open(dFullName,'wb').write)
                else:
                    ftpObj.cwd(dName)
                    cdir1=cdir+"\\"+dName
                    print("\nEntering ",cdir1)
                    traverseFTP(ftpObj,depth+1,cdir1,bdir)
                    ftpObj.cwd("..")
                    print("Exiting ",cdir1)

def ftpInitialize(ftpHost,userID,userPW):
    ftpObj=ftplib.FTP(ftpHost)
    ftpObj.login(user=userID,passwd=userPW)
    return ftpObj

def main():
    ftpHost="hostname"
    userID="userlogin"
    userPW="userpassword"
    targetDir="D:\\LocalDir"
    ftpObj=ftplib.FTP(ftpHost)
    ftpObj.login(user=userID,passwd=userPW)
    traverseFTP(ftpObj,0,"",targetDir)
    ftpObj.quit()

if __name__=="__main__":
    main()

请注意:请考虑我是一个Python初学者(因此详细的答案将不胜感激)。在

另外,Python3.3的ftplib中是否对文件大小(下载文件的大小)有任何限制?我的代码通常挂在大于0.5gb的大小上,但在0.5gb的大小上运行良好。我正在64位Windows7 pc上使用WinPython-64bit-3.3.2.1发行版(即64位)


Tags: 文件ifosprintentryuseriddepthftplib