Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python3, ftplib storlines error

I want to upload an ASCII file. This 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?

like image 945
tommy.carstensen Avatar asked Jul 21 '13 22:07

tommy.carstensen


1 Answers

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

"Lines are read until EOF from the file object file (opened in binary mode) using its readline() method to provide the data to be stored."

So I just had to open in binary mode:

ftp.storlines('STOR ' + 'file.htm', open('file.htm','rb'))
like image 97
tommy.carstensen Avatar answered Oct 27 '22 09:10

tommy.carstensen