-2

I am trying to read the contents of text files on an FTP server, my code can make a connection and list all files present in the directory. However, it is failing here:

from io import BytesIO
import os

r = BytesIO()
ftp.retrbinary("RETR " + "textfile.txt", r)    
print (r)

This prints:

<_io.BytesIO object at 0x03C2CCF0>

Firstly I'm not entirely sure what this means so If someone could shed some light on that I would be grateful.

Also when I try the following code to decode the returned value I get this error:

TypeError: '_io.BytesIO' object is not callable

Code looks like this:

r = BytesIO()
ftp.retrbinary("RETR " + "textfile.txt", r)    
print (r.decode("utf-8"))
EcSync
  • 842
  • 1
  • 6
  • 20

1 Answers1

1

Try r.write and r.getvalue()

from io import BytesIO
from ftplib import FTP
import os

ftp = FTP('ftp.test.org') 
ftp.login()

r = BytesIO()
ftp.retrbinary("RETR " + "textfile.txt", r.write)    
print (r.getvalue())

https://docs.python.org/3/library/io.html

Kvothe
  • 110
  • 4