【问题标题】:Python FTP download file that has spaces in the filename文件名中包含空格的 Python FTP 下载文件
【发布时间】:2019-12-23 21:19:52
【问题描述】:

上周我们的测量设备供应商更新了固件。 我在这里总是使用 python 脚本来下载所有 .rd 文件。

固件正式返回的文件名总是像191222_221400.rd 一个完整的文件名。

不幸的是,更新后固件将其返回为"rw group owner oct 9_191222_221400.rd"

脚本现在停止写入的地方。然后不删除它。

## Handles to download and delete the .rd file in the project folder
def downloadAndDelet_RD(self):
    time.sleep(5)
    files = []
    #print self.conf.INSipAddress
    ftp = ftplib.FTP(self.conf.INSipAddress)
    ftp.login()
    files =  ftp.nlst()
    for f in files:
        if f.endswith(".rd"):
            #print self.conf.newP + '//'  + self.conf.nativefolderName + '//' +self.gpsSelectedProject+"//"+f
            ftpfile = urllib2.urlopen("ftp://"+self.conf.INSipAddress +"//"+f)
            localfile = open(self.conf.newP + '//'  + self.conf.nativefolderName + '//' +self.gpsSelectedProject+"//"+f, "wb")        
            ftp.retrbinary('RETR %s' % f, localfile.write)
            localfile.close()
            if self.conf.INSDeleteRD == 1:
                ftp.delete(f)

我已经尝试了各种方法让它接受全名,但我被卡住了。并且无处可去。

关于如何解决此问题的任何建议?

【问题讨论】:

  • 您使用的是 Python 2,对吗?
  • 是的,正确的 Python 2
  • 代码是否抛出任何错误?刚刚运行成功,但是输出文件是空的?
  • 这就是问题所在,没有错误,只是“挂起”。它开始下载。它从哪里获得名称,但没有进一步的 0KB 和名称。
  • 你知道它挂在哪里吗?

标签: python ftp python-2.x ftplib


【解决方案1】:

你没有提供我要求的信息。无论如何,根据我们掌握的信息:

您的 FTP 服务器似乎已损坏,它返回完整的目录列表,包括 NLST 响应中的元数据。这违反了 FTP 规范。我认为您的代码没有任何问题。您必须通过从 NLST 响应中删除元数据来解决该错误。

f = f.split()[-1]

【讨论】:

    【解决方案2】:

    我一生中从未使用过 Python 2x。我玩游戏有点晚,所以我一直使用 Python 3x。下面的脚本在 Python 3x 中对我来说很好。

    import ftplib
    from ftplib import FTP
    ftp = FTP()
    from datetime import datetime
    
    
    filenames = []
    data = []
    
    ftp = ftplib.FTP('ftp.anything.com', 'u_name', 'ps_wd')  
    
    
    def get_dirs_ftp(folder=""):
        contents = ftp.nlst(folder)
        folders = []
        for item in contents:
            if "." not in item:
                folders.append(item)
        return folders
    def get_all_dirs_ftp(folder=""):
        dirs = []
        new_dirs = []
        new_dirs = get_dirs_ftp(folder)
        while len(new_dirs) > 0:
            for dir in new_dirs:
                dirs.append(dir)
    
            old_dirs = new_dirs[:]
            new_dirs = []
            for dir in old_dirs:
                for new_dir in get_dirs_ftp(dir):
                    new_dirs.append(new_dir)
        dirs.sort()
        return dirs
    
    #allfiles = []
    # get parent and child folders in directory
    all_dirs = get_all_dirs_ftp()
    
    # create a list to append metadata
    dir_list = []
    
    for dir in all_dirs:
        ftp.cwd('/'+dir+'/')
        print(dir)
        dir_list.append(dir)
        ftp.dir(dir_list.append)
    
        len(dir_list)
    
    
    # you probably want to dump the results to a file...
    outF = open('C:/your_path/filenames.csv', 'w')
    for line in dir_list:
      # write line to output file
      outF.write(line)
      outF.write("\n")
    outF.close()
    print('Done!!')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-05
      • 2011-11-20
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多