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
ftplib
library. - Sets the
path
variable to/debian/
and thefilename
variable toREADME.html
. - Creates an FTP object using the
FTP
constructor from theftplib
library, passing in the hostname ("ftp.fi.debian.org"
) as an argument. - Calls the
login()
method of theftp
object to log in to the FTP server. - Calls the
cwd()
method of theftp
object to change the current working directory to/debian/
. - Calls the
retrlines()
method of theftp
object to list the files in the current directory. - Calls the
retrbinary()
method of theftp
object to download theREADME.html
file 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 theftp
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