Error python3, ftplib

I want to download an ASCII file. This was used to work in 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() 

However, in Python 3, it returns this error:

  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 

What am I doing wrong?

+4
source share
1 answer

I read the documentation: http://docs.python.org/3/library/ftplib.html#ftplib.FTP.storlines

"Lines are read before EOF from a file object file (opened in binary mode) using the readline () method to provide data storage."

So I just needed to open in binary mode:

 ftp.storlines('STOR ' + 'file.htm', open('file.htm','rb')) 
+5
source

Source: https://habr.com/ru/post/1492615/


All Articles