Python3: ftp file upload throws TypeError: Type str doesn't support the buffer API
Python3: ftp file upload throws TypeError: Type str doesn't support the buffer API
我正在使用 python 3.4.2 将日志文件上传到 ftp 服务器(为了简单起见,我删除了 try 和 with 语句):
import ftplib
ftp = ftplib.FTP(url)
ftp.login(name, password)
ftp.storlines("STOR " + "mylog.log", open("log/mylog.log"))
ftp.close()
mylog.log 文件具有 "text/plain; charset=us-ascii" 编码。 在我的 macbook 上一切正常。 当我在 raspberry pi(通过 ssh)上执行我的小程序时,我收到以下错误消息:
Traceback (most recent call last):
File "./ftptest.py", line 7, in <module>
ftp.storlines("STOR " + "mylog.log", open("log/mylog.log"))
File "/usr/lib/python3.4/ftplib.py", line 537, in storlines
if buf[-1] in B_CRLF: buf = buf[:-1]
TypeError: Type str doesn't support the buffer API
我猜这是一些编码 and/or 本地设置问题。我究竟做错了什么?这里的最佳做法是什么?
根据我对 http://bugs.python.org/issue6822 的阅读,我认为 Python 3.x,你需要 open("log/mylog.log", "rb")
。
我正在使用 python 3.4.2 将日志文件上传到 ftp 服务器(为了简单起见,我删除了 try 和 with 语句):
import ftplib
ftp = ftplib.FTP(url)
ftp.login(name, password)
ftp.storlines("STOR " + "mylog.log", open("log/mylog.log"))
ftp.close()
mylog.log 文件具有 "text/plain; charset=us-ascii" 编码。 在我的 macbook 上一切正常。 当我在 raspberry pi(通过 ssh)上执行我的小程序时,我收到以下错误消息:
Traceback (most recent call last):
File "./ftptest.py", line 7, in <module>
ftp.storlines("STOR " + "mylog.log", open("log/mylog.log"))
File "/usr/lib/python3.4/ftplib.py", line 537, in storlines
if buf[-1] in B_CRLF: buf = buf[:-1]
TypeError: Type str doesn't support the buffer API
我猜这是一些编码 and/or 本地设置问题。我究竟做错了什么?这里的最佳做法是什么?
根据我对 http://bugs.python.org/issue6822 的阅读,我认为 Python 3.x,你需要 open("log/mylog.log", "rb")
。