How to download a file via FTP with Python ftplib

140,803

Solution 1

handle = open(path.rstrip("/") + "/" + filename.lstrip("/"), 'wb')
ftp.retrbinary('RETR %s' % filename, handle.write)

Solution 2

A = filename

ftp = ftplib.FTP("IP")
ftp.login("USR Name", "Pass")
ftp.cwd("/Dir")


try:
    ftp.retrbinary("RETR " + filename ,open(A, 'wb').write)
except:
    print "Error"

Solution 3

FILENAME = 'StarWars.avi'    

with ftplib.FTP(FTP_IP, FTP_LOGIN, FTP_PASSWD) as ftp:
    ftp.cwd('movies')
    with open(FILENAME, 'wb') as f:
        ftp.retrbinary('RETR ' + FILENAME, f.write)

Of course it would we be wise to handle possible errors.

Solution 4

Please note if you are downloading from the FTP to your local, you will need to use the following:

with open( filename, 'wb' ) as file :
        ftp.retrbinary('RETR %s' % filename, file.write)

Otherwise, the script will at your local file storage rather than the FTP.

I spent a few hours making the mistake myself.

Script below:

import ftplib

# Open the FTP connection
ftp = ftplib.FTP()
ftp.cwd('/where/files-are/located')


filenames = ftp.nlst()

for filename in filenames:

    with open( filename, 'wb' ) as file :
        ftp.retrbinary('RETR %s' % filename, file.write)

        file.close()

ftp.quit()

Solution 5

The ftplib module in the Python standard library can be compared to assembler. Use a high level library like: https://pypi.python.org/pypi/ftputil

Share:
140,803
Intekhab Khan
Author by

Intekhab Khan

Programmer by Mind | Designer by Process | Manager by plan

Updated on July 05, 2022

Comments

  • Intekhab Khan
    Intekhab Khan almost 2 years

    I have the following code which easily connects to the FTP server and opens a zip file. I want to download that file into the local system. How to do that?

    # Open the file for writing in binary mode
    print 'Opening local file ' + filename
    file = open(filename, 'wb')
    
    # Download the file a chunk at a time
    # Each chunk is sent to handleDownload
    # We append the chunk to the file and then print a '.' for progress
    # RETR is an FTP command
    
    print 'Getting ' + filename
    ftp.retrbinary('RETR ' + filename, handleDownload)
    
    # Clean up time
    print 'Closing file ' + filename
    file.close()
    
    • Lekensteyn
      Lekensteyn almost 11 years
      I suggest the use of with here which takes care of closing the file handle when done: with open(filename, "wb") as file: ftp.retrbinary("RETR " + filename, file.write)
    • pnovotnak
      pnovotnak almost 9 years
      FD leaks are no joke! While you're at it, you might rename file to f, since file shadows the builtin file .
    • Jossie Calderon
      Jossie Calderon almost 8 years
      use retrlines if the file is a text file.
  • chuckjones242
    chuckjones242 about 7 years
    Thanks, this was a great help to me for getting started with file handling, even with comments in espanol :)
  • chill_turner
    chill_turner about 6 years
    could use some context . ideally as others mention you call this command inside a with statement to manage your file descriptors and automatically close it out for you!
  • LOKE2707
    LOKE2707 almost 5 years
    what is i in open(i,'wb').write?
  • LOKE2707
    LOKE2707 almost 5 years
    how to access that file? lets say i have a csv file in my ftp that i want to open and store as a dataframe, how do I do that?
  • Mathias711
    Mathias711 over 4 years
    @LOKE2707 it was the filename, declared in the first line. I changed it. Thanks for noting it
  • Mateus da Silva Teixeira
    Mateus da Silva Teixeira almost 4 years
    Thank you, mainly for the example using 'try'. Help me a lot!
  • Martin Prikryl
    Martin Prikryl about 3 years
    As @chill_turner has commented, this does not even close the local file. For a robust example, see the answer by @RdB.
  • yzorg
    yzorg over 2 years
    Source is hosted on author's personal domain, no readme on pypi. A few releases in the last year, so looks maintained, +1. But the website (docs) are look very old and a bit janky, -1. Is the author known by you or to the community? Python is huge, so any sub-community would do.
  • Debjyoti Banerjee
    Debjyoti Banerjee over 2 years
    This works fine for files of smaller size, but for files of larger size, the whole thing seems to stuck. Do you know how to solve that?
  • variable
    variable over 2 years
    Hi - I am looking at the ftplib code (github.com/python/cpython/blob/…) but can't figure out how this (line 354) connection is closed. Can you help me by pointing to the line that closes the socket?