Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nasrulhazim/cfd5f01e3b261b09d54f721cc1a7c50d to your computer and use it in GitHub Desktop.
Save nasrulhazim/cfd5f01e3b261b09d54f721cc1a7c50d to your computer and use it in GitHub Desktop.
Download Files From FTP Server using Python3
from ftplib import FTP
from datetime import datetime

start = datetime.now()
ftp = FTP('your-ftp-domain-or-ip')
ftp.login('your-username','your-password')

# Get All Files
files = ftp.nlst()

# Print out the files
for file in files:
	print("Downloading..." + file)
	ftp.retrbinary("RETR " + file ,open("download/to/your/directory/" + file, 'wb').write)

ftp.close()

end = datetime.now()
diff = end - start
print('All files downloaded for ' + str(diff.seconds) + 's')`
@m1hk3l
Copy link

m1hk3l commented Mar 28, 2020

Thank you! You are awesome.

@Angileca
Copy link

Angileca commented May 6, 2021

Thank you you did great.
Regards
Saiful

@debjyoti003
Copy link

This works fine if the file size is small, but when you try to download larger files using this code, it will fail.

@scrwho
Copy link

scrwho commented May 16, 2022

This is cool.
My files are in subdirectories of the ftp I am trying to download, how do I download all directory with the files in them.

@kevinu89
Copy link

kevinu89 commented Oct 7, 2022

I have tried this but am failing at understanding how to indicate the path to the location to put the files. I can see the list of files in variable "files", but I cannot transfer them to my local drive.

In this example I tried to indicate the path:

Print out the files
for file in files:
print("Downloading..." + file)
ftp.retrbinary("RETR " + file ,open("c:\python_work\ftp" + file, 'wb').write)

but I get this error:

OSError: [Errno 22] Invalid argument: 'c:\python_work\x0ctp.'

@Dollysi
Copy link

Dollysi commented Nov 3, 2022

this is the code for downloading all files, what if i want to download in selected date range?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment