尝试使用ftplib上传,可以很好地连接,但我在某些方面搞砸了。

时间:2014-12-02 18:46:31

标签: python

这里是我的代码,它引用了一个未显示的类。 class_connector只提供一个模板来收集连接凭据。

import connect_class
from ftplib import FTP
import os

account = connect_class.connector(raw_input('enter your hostname: '),
                          raw_input('enter your user name: '),
                          raw_input('enter your password: '),
                          raw_input('enter your directory: '))


ftp = FTP(account.host)
ftp.login(user=account.user, passwd=account.password)
ftp.retrlines('LIST')

def upload(ftp, file):
    ext = os.path.splitext(file)[1]
    if ext in (".txt", ".htm", ".html"):
        ftp.storlines("STOR " + file, open(file))
    else:
        ftp.storbinary("STOR " + file, open(file, "rb"), 1024)

upload(ftp, '/file/somefile/file')

1 个答案:

答案 0 :(得分:0)

你正在传递一条完整的道路。您可能希望使用basename作为远程文件名。

basename = os.path.basename(file)
if ext in (".txt", ".htm", ".html"):
    ftp.storlines("STOR " + basename, open(file))
else:
    ftp.storbinary("STOR " + basename, open(file, "rb"), 1024)