Python 尝试用ftplib上传,可以很好地连接,但我在某个点上搞砸了。

Python 尝试用ftplib上传,可以很好地连接,但我在某个点上搞砸了。,python,Python,这是我的代码,它引用了一个未显示的类。class_连接器只是提供一个模板来收集连接凭据 import connect_class from ftplib import FTP import os account = connect_class.connector(raw_input('enter your hostname: '), raw_input('enter your user name: '),

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

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')

您正以完整的路径通过。您可能希望使用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)