5

我想上传一个 ASCII 文件。这曾经在 Python 2 中工作:

ftp = ftplib.FTP('ftp.domain.com')
ftp.login('domain.com',password)
ftp.cwd('subdirectory')
ftp.storlines('STOR ' + 'file.htm', open('file.htm','r'))
ftp.close()

但是,在 Python 3 中,它返回此错误:

  File "/usr/local/lib/python3.3/ftplib.py", line 497, in storlines
    if buf[-1] in B_CRLF: buf = buf[:-1]
TypeError: Type str doesn't support the buffer API

我究竟做错了什么?

4

1 回答 1

6

我阅读了文档: http ://docs.python.org/3/library/ftplib.html#ftplib.FTP.storlines

“使用其 readline() 方法从文件对象文件(以二进制模式打开)中读取行直到 EOF,以提供要存储的数据。”

所以我只需要以二进制模式打开:

ftp.storlines('STOR ' + 'file.htm', open('file.htm','rb'))
于 2013-07-21T22:52:36.413 回答