Python: Download a File from an Ftp Server

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)

Downloading file from FTP server with ftplib: Always 0 bytes/empty

You are not closing the local file after the download. You should use context manager for that. Similarly also for the FTP session:

with ftplib.FTP_TLS(server) as session:
session.login(user=username, passwd=password)
session.prot_p()
session.set_pasv(False)
session.nlst()
session.cwd("home")
print(session.pwd())
filename = "test.txt"
# Open a local file to store the downloaded file
with open(r'c:\temp\ftpTest.txt', 'wb') as my_file:
session.retrbinary('RETR ' + filename, my_file.write, 1024)

How to download specified extension type of files from FTP server using python

Something like this should work fine:

import fnmatch

with FTP(FTP_HOST,FTP_USER,FTP_PASS) as ftp:
ftp.encoding = "utf-8"
ftp.cwd('/files')
for filename in ftp.nlst():
if fnmatch.fnmatch(filename, '*.txt'):
with open(filename, 'wb') as fp:
ftp.retrbinary(f'RETR {filename}', fp.write)

Using Python, how to download multiple files from a subdirectory on FTP server into a desired directory on local machine?

You have to provide a full path to open function, not just a directory name.

To assemble a full local path, take a file name from the remote paths returned by ftp.nlst and combine them with the target local directory path.

Like this:

local_fn = os.path.join(local_path, os.path.basename(file))

How to download a file on FTP by using Python

As answered in the comment, it seems that you have used the file path /users/appli/gedftp/data/BO/LME/TEST_CSV_INTE.csv in your local code and trying to open it on your local machine, while you were actually referencing a file which is located on server.

That's why opening the file locally failed :)

How to download a file via FTP with Python ftplib

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

Download files from an FTP server containing given string using Python

Ok, seems to work. There may be issues if trying to download a directory, or scan a file. Exception handling may come handy to trap wrong filetypes and skip.

glob.glob cannot work since you're on a remote filesystem, but you can use fnmatch to match the names

Here's the code: it download all files matching *DEM* in TEMP directory, sorting by directory.

import ftplib,sys,fnmatch,os

output_root = os.getenv("TEMP")

fc = ftplib.FTP("ftp.igsb.uiowa.edu")
fc.login()
fc.cwd("/gis_library/counties")

root_dirs = fc.nlst()
for l in root_dirs:
sys.stderr.write(l + " ...\n")
#print(fc.size(l))
dir_files = fc.nlst(l)
local_dir = os.path.join(output_root,l)
if not os.path.exists(local_dir):
os.mkdir(local_dir)

for f in dir_files:
if fnmatch.fnmatch(f,"*DEM*"): # cannot use glob.glob
sys.stderr.write("downloading "+l+"/"+f+" ...\n")
local_filename = os.path.join(local_dir,f)
with open(local_filename, 'wb') as fh:
fc.retrbinary('RETR '+ l + "/" + f, fh.write)

fc.close()


Related Topics



Leave a reply



Submit