ftplib文件选择 [英] ftplib file select

查看:226
本文介绍了ftplib文件选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用手册说明


要下载文件,请使用ftp.retrlines('RETR'+文件名)

To download a file, use ftp.retrlines('RETR ' + filename)

这是我做的:

Here is what i do:

ftp.retrbinary('RETR media/backups/andrey.txt', open("file_to_get.txt", 'a+').write)

可否请某人建议如何在RETR命令后面添加通过raw_input添加的文件名变量?
使用%s但这不起作用,它会像文件名的一部分那样处理。

Could please someone advise how to put filename variable, previously added via raw_input, after the "RETR" command? Used %s but this does not work, it gets processed like a part of a filename.

ftp.retrbinary('RETR %s', open("file_to_get.txt", 'a+').write) %raw_input("print file name")

产生这样的结果:

produces this:

c:\>python ftp_client.py
Enter host95.31.8.52
drwxrwxrwx   61 99         102             32768 Sep 14 01:39 backpl
drwxrwxrwx   19 99         102              4096 Sep  7 13:47 media
drwxrwxrwx    2 99         102              4096 Jul  2 11:15 naswebsite
Traceback (most recent call last):
  File "ftp_client.py", line 12, in <module>
    ftp.retrbinary('RETR %s', open("file_to_get.txt", 'a+').write) %raw_input("p
rint file name")
  File "C:\Python27\lib\ftplib.py", line 399, in retrbinary
    conn = self.transfercmd(cmd, rest)
  File "C:\Python27\lib\ftplib.py", line 361, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "C:\Python27\lib\ftplib.py", line 330, in ntransfercmd
    resp = self.sendcmd(cmd)
  File "C:\Python27\lib\ftplib.py", line 244, in sendcmd
    return self.getresp()
  File "C:\Python27\lib\ftplib.py", line 219, in getresp
    raise error_perm, resp
ftplib.error_perm: 550 Can't open %s: No such file or directory

c:\>


推荐答案


filename = 'andrey.txt'
path = "media/backups/"
ftp.cwd(path)
ftp.retrbinary("RETR " + filename ,open(filename, 'a+').write)

这篇关于ftplib文件选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆