I'm a newbie in Python programming. My question is, how to download a few files at the same time. Not file by file but simultaneously from one directory on ftp. Now I use this script but I don't know how I can rebuild this code:
filenames = []
ftp.retrlines("NLST", filenames.append)
print filenames
print path
for filename in filenames:
local_filename = filename
print filename
print local_filename
f = open(local_filename, "wb")
s = ftp.size(local_filename)
sMB = s/(1024*1024)
print "file name: " + local_filename + "\nfile size: " + str(sMB) + " MB"
ftp.retrbinary("RETR %s" % local_filename, f.write)
print "\n Done :) "
time.sleep(2)
f.close()
ftp.quit() #closing connection
time.sleep(5)
It works fine, but not what I need.
You could use multiple threads or processes. Make sure you create a new
ftplib.FTP
object in each thread. The simplest way (code-wise) is to usemultiprocessing.Pool
:where
urls
contains ftp urls for the files to download e.g.,ftp://example.com/path/to/file
andurl2filename()
extracts the filename part from an url e.g.: