Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download a file via FTP with Python ftplib

Tags:

python

ftp

ftplib

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() 
like image 203
Intekhab Khan Avatar asked Jul 20 '12 06:07

Intekhab Khan


People also ask

How do I open a FTP file in Python?

The ftplib module included in Python allows you to use Python scripts to quickly attach to an FTP server, locate files, and then download them to be processed locally. To open a connection to the FTP server, create an FTP server object using the ftplib. FTP([host [, user [, passwd]]]) method.


2 Answers

handle = open(path.rstrip("/") + "/" + filename.lstrip("/"), 'wb') ftp.retrbinary('RETR %s' % filename, handle.write) 
like image 52
number23_cn Avatar answered Oct 13 '22 13:10

number23_cn


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" 
like image 29
Rakesh Avatar answered Oct 13 '22 15:10

Rakesh