How to download a file from ftp?

I am writing a script installation script in python.

How to upload a file from ftp in python?

The operating system is Windows XP - if that matters.

+4
source share
4 answers

Here is the code snippet I'm using right now.

import mimetypes import os import urllib2 import urlparse def filename_from_url(url): return os.path.basename(urlparse.urlsplit(url)[2]) def download_file(url): """Create an urllib2 request and return the request plus some useful info""" name = filename_from_url(url) r = urllib2.urlopen(urllib2.Request(url)) info = r.info() if 'Content-Disposition' in info: # If the response has Content-Disposition, we take filename from it name = info['Content-Disposition'].split('filename=')[1] if name[0] == '"' or name[0] == "'": name = name[1:-1] elif r.geturl() != url: # if we were redirected, take the filename from the final url name = filename_from_url(r.geturl()) content_type = None if 'Content-Type' in info: content_type = info['Content-Type'].split(';')[0] # Try to guess missing info if not name and not content_type: name = 'unknown' elif not name: name = 'unknown' + mimetypes.guess_extension(content_type) or '' elif not content_type: content_type = mimetypes.guess_type(name)[0] return r, name, content_type 

Using:

 req, filename, content_type = download_file('http://some.url') 

You can then use req as a file-like object and, for example, use shutil.copyfileobj() to copy the contents of the file to a local file. If the MIME type does not matter, simply delete this part of the code.

Since you seem to be lazy, here is the code loading the file directly to the local file:

 import shutil def download_file_locally(url, dest): req, filename, content_type = download_file(url) if dest.endswith('/'): dest = os.path.join(dest, filename) with open(dest, 'wb') as f: shutil.copyfileobj(req, f) req.close() 

This method is smart enough to use the file name sent by the server if you specify a path ending with a slash, otherwise it uses the destination you specified.

+3
source
 from urllib2 import urlopen req = urlopen('ftp://ftp.gnu.org/README') 

You can then use req.read() to load the contents of the file into a variable or do something else with it or shutil.copyfileobj to save the contents to disk without loading it into memory.

+6
source

Use ftplib

Example code from the documentation:

 >>> from ftplib import FTP >>> ftp = FTP('ftp.cwi.nl') # connect to host, default port >>> ftp.login() # user anonymous, passwd anonymous@ >>> ftp.retrlines('LIST') # list directory contents total 24418 drwxrwsr-x 5 ftp-usr pdmaint 1536 Mar 20 09:48 . dr-xr-srwt 105 ftp-usr pdmaint 1536 Mar 21 14:32 .. -rw-r--r-- 1 ftp-usr pdmaint 5305 Mar 20 09:48 INDEX . . . >>> ftp.retrbinary('RETR README', open('README', 'wb').write) '226 Transfer complete.' >>> ftp.quit() 
+1
source
 from urllib.request import urlopen try: req = urlopen('ftp://ftp.expasy.org/databases/enzyme/enzclass.txt') except: print ("Error") 
0
source

All Articles