python - ftplib.error_perm : 500 not understood downloading file from ftp

标签 python

我是 python 新手,尝试从 ftp 文件夹下载所有 csv 文件,但收到此错误。 这是我的代码:

ftp = ftplib.FTP('192.128.0.20', 'bingo', 'Password')
ftp.cwd('/')
filematch = '*.csv'


for filename in ftp.nlst(filematch):
    fhandle = open(filename,'wb')
    ftp.retrbinary('RETR' + filename, fhandle.write)
    fhandle.close()

这是我的错误:

 Traceback (most recent call last):
      File "./dirmon.py", line 65, in <module>
        ftp.retrbinary('RETR' + filename, fhandle.write)
      File "/usr/lib/python2.7/ftplib.py", line 406, in retrbinary
        conn = self.transfercmd(cmd, rest)
      File "/usr/lib/python2.7/ftplib.py", line 368, in transfercmd
        return self.ntransfercmd(cmd, rest)[0]
      File "/usr/lib/python2.7/ftplib.py", line 331, in ntransfercmd
        resp = self.sendcmd(cmd)
      File "/usr/lib/python2.7/ftplib.py", line 244, in sendcmd
        return self.getresp()
      File "/usr/lib/python2.7/ftplib.py", line 219, in getresp
        raise error_perm, resp
    ftplib.error_perm: 500 RETRRINGGODATA-2014-07-02.CSV not understood

最佳答案

ftp.retrbinary 处缺少空格:(我也更喜欢 with 声明):

ftp = ftplib.FTP('192.128.0.20', 'bingo', 'Password')
ftp.cwd('/')
filematch = '*.csv'
target_dir = '/home/toor/ringolist'
import os

for filename in ftp.nlst(filematch):
    target_file_name = os.path.join(target_dir, os.path.basename(filename))
    with open(target_file_name ,'wb') as fhandle:
        ftp.retrbinary('RETR %s' % filename, fhandle.write)

关于python - ftplib.error_perm : 500 not understood downloading file from ftp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24733125/

相关文章:

python - 如何添加按键绑定(bind)?

python - 安装 TensorFlow 时出现问题

python - jupyter 笔记本不在 GitHub 要点中呈现

python - 使用ansible检查python模块版本

python - 如何使用python连接文件夹中的所有音频(.wav)文件?

python - 使用 pymongo 在 MongoDB 中选择 * limit 100 等值

python - 带有 Python 错误的 OpenCV - binary_op 中的断言失败((mask.type()== CV_8UC1 || mask.type()== CV_8SC1))

python - 将执行顺序与打印函数调用作为函数参数混淆

python 程序在 python shell 和终端中的行为不同

python - 将多个参数传递给函数的最有效方法?