Is it possible to read FTP files without writing them using Python?
Well, you have the answer right in front of you: The FTP.retrbinary
method accepts as second parameter a reference to a function that is called whenever file content is retrieved from the FTP connection.
Here is a simple example:
#!/usr/bin/env pythonfrom ftplib import FTPdef writeFunc(s): print "Read: " + sftp = FTP('ftp.kernel.org') ftp.login()ftp.retrbinary('RETR /pub/README_ABOUT_BZ2_FILES', writeFunc)
You should implement writeFunc
so that it actually appends the data read to an internal variable, something like this, which uses a callable object:
#!/usr/bin/env pythonfrom ftplib import FTPclass Reader: def __init__(self): self.data = "" def __call__(self,s): self.data += sftp = FTP('ftp.kernel.org') ftp.login()r = Reader()ftp.retrbinary('RETR /pub/README_ABOUT_BZ2_FILES', r)print r.data
Update: I realized that there is a module in the Python standard library that is meant for this kind of things, BytesIO
:
#!/usr/bin/env pythonfrom ftplib import FTPfrom io import BytesIOftp = FTP('ftp.kernel.org') ftp.login()r = BytesIO()ftp.retrbinary('RETR /pub/README_ABOUT_BZ2_FILES', r.write)print r.getvalue()