Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8140071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:02:36+00:00 2026-06-06T12:02:36+00:00

I am trying to read files using Python’s ftplib without writing them. Something roughly

  • 0

I am trying to read files using Python’s ftplib without writing them. Something roughly equivalent to:

def get_page(url):
    try:
        return urllib.urlopen(url).read()
    except:
        return ""

but using FTP.

I tried:

def get_page(path):
    try:
        ftp = FTP('ftp.site.com', 'anonymous', 'passwd')
        return ftp.retrbinary('RETR '+path, open('page').read())
    except:
        return ''

but this doesn’t work. The only examples in the docs involve writing files using the ftp.retrbinary('RETR README', open('README', 'wb').write) format. Is it possible to read ftp files without writing first?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:02:37+00:00Added an answer on June 6, 2026 at 12:02 pm

    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 python
    from ftplib import FTP
    
    def writeFunc(s):
      print "Read: " + s
    
    ftp = 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 python
    from ftplib import FTP
    
    class Reader:
      def __init__(self):
        self.data = ""
      def __call__(self,s):
         self.data += s
    
    ftp = 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 python
    from ftplib import FTP
    from io import BytesIO
    
    ftp = FTP('ftp.kernel.org') 
    ftp.login()
    r = BytesIO()
    ftp.retrbinary('RETR /pub/README_ABOUT_BZ2_FILES', r.write)
    
    print r.getvalue()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Python (3.1 or 2.6), I'm trying to read data from binary data files
I'm trying to copy files inside a Python script using the following code: inf,outf
I'm trying to develop simple Python (3.2) code to read XML files, do some
I have a JSON object that I am trying to read using Python but
I'm trying to upload files to the blobstore in my Google App without using
I am trying to read binary data from sys.stdin using Python 2.7 on Windows
I'm trying to read some files using Python3.2, the some of the files may
I am trying to read a remote file using HttpWebRequest in a C# Console
I am trying to read contents of a file using string tokenizer and store
I am trying to read values from a text file using the Qt code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.