Sunday, April 20, 2025

Download a File From Public FTP Server - Python

import ftplib

path = '/debian/'
filename = 'README.html'

ftp = ftplib.FTP("ftp.fi.debian.org") 
ftp.login()
ftp.cwd(path)
ftp.retrlines('LIST')
ftp.retrbinary("RETR " + filename ,open(filename, 'wb').write)
ftp.quit()

This script uses the ftplib library to download a file named README.html from an FTP server located at 'ftp.fi.debian.org', within the /debian/ directory.

Here's what the code does:

  1. Imports the ftplib library.
  2. Sets the path variable to /debian/ and the filename variable to README.html.
  3. Creates an FTP object using the FTP constructor from the ftplib library, passing in the hostname ("ftp.fi.debian.org") as an argument.
  4. Calls the login() method of the ftp object to log in to the FTP server.
  5. Calls the cwd() method of the ftp object to change the current working directory to /debian/.
  6. Calls the retrlines() method of the ftp object to list the files in the current directory.
  7. Calls the retrbinary() method of the ftp object to download the README.html file in binary mode, using the open() function to create a file object for writing, and the write() method to write the downloaded data to the file.
  8. Calls the quit() method of the ftp object to disconnect from the FTP server.

After running this script, the README.html file will be downloaded and saved in the current working directory.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...