Python: download a file from an FTP server
The requests
library doesn't support ftp://
links.
To download a file from an FTP server you could use urlretrieve
:
import urllib.request
urllib.request.urlretrieve('ftp://server/path/to/file', 'file')
# if you need to pass credentials:
# urllib.request.urlretrieve('ftp://username:password@server/path/to/file', 'file')
Or urlopen
:
import shutil
import urllib.request
from contextlib import closing
with closing(urllib.request.urlopen('ftp://server/path/to/file')) as r:
with open('file', 'wb') as f:
shutil.copyfileobj(r, f)
Python 2:
import shutil
import urllib2
from contextlib import closing
with closing(urllib2.urlopen('ftp://server/path/to/file')) as r:
with open('file', 'wb') as f:
shutil.copyfileobj(r, f)
Use urllib2. For more specifics, check out this example from doc.python.org:
Here's a snippet from the tutorial that may help
import urllib2
req = urllib2.Request('ftp://example.com')
response = urllib2.urlopen(req)
the_page = response.read()
You Can Try this
import ftplib
path = 'pub/Health_Statistics/NCHS/nhanes/2001-2002/'
filename = 'L28POC_B.xpt'
ftp = ftplib.FTP("Server IP")
ftp.login("UserName", "Password")
ftp.cwd(path)
ftp.retrbinary("RETR " + filename, open(filename, 'wb').write)
ftp.quit()
Try using the wget library for python. You can find the documentation for it here.
import wget
link = 'ftp://example.com/foo.txt'
wget.download(link)