write python coding to transfer files via ftp
Here is an example of Python code to transfer files via FTP:
import ftplib
ftp_server = 'ftp.example.com'
ftp_username = 'username'
ftp_password = 'password'
# Connect to FTP server
ftp = ftplib.FTP(ftp_server)
ftp.login(user=ftp_username, passwd=ftp_password)
# Change to the desired directory
ftp.cwd('/path/to/directory')
# Upload a file to the FTP server
with open('file.txt', 'rb') as file:
ftp.storbinary('STOR file.txt', file)
# Download a file from the FTP server
with open('file.txt', 'wb') as file:
ftp.retrbinary('RETR file.txt', file.write)
# Close the FTP connection
ftp.quit()
In this example, we first specify the FTP server address, username, and password. Then, we connect to the server using the ftplib.FTP function and log in with the provided credentials.
Next, we change to the desired directory on the server using ftp.cwd(). We then upload a file to the server using ftp.storbinary(), which takes the file name and a file object opened in read-binary mode.
Finally, we download a file from the server using ftp.retrbinary(), which takes the file name and a file object opened in write-binary mode.
After we are done with the FTP operations, we close the connection using ftp.quit().
原文地址: https://www.cveoy.top/t/topic/lTa 著作权归作者所有。请勿转载和采集!