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:
- Imports the
ftpliblibrary. - Sets the
pathvariable to/debian/and thefilenamevariable toREADME.html. - Creates an FTP object using the
FTPconstructor from theftpliblibrary, passing in the hostname ("ftp.fi.debian.org") as an argument. - Calls the
login()method of theftpobject to log in to the FTP server. - Calls the
cwd()method of theftpobject to change the current working directory to/debian/. - Calls the
retrlines()method of theftpobject to list the files in the current directory. - Calls the
retrbinary()method of theftpobject to download theREADME.htmlfile in binary mode, using theopen()function to create a file object for writing, and thewrite()method to write the downloaded data to the file. - Calls the
quit()method of theftpobject to disconnect from the FTP server.
After running this script, the README.html file will be downloaded and saved in the current working directory.