要使用Python快速传输文件,可以使用ftplib
库
from ftplib import FTP def upload_file(local_file, remote_file, host, user, password): # 连接到FTP服务器 ftp = FTP() ftp.connect(host) ftp.login(user, password) ftp.set_pasv(True) # 被动模式 # 打开本地文件并上传 with open(local_file, 'rb') as f: ftp.storbinary(f'STOR {remote_file}', f) # 断开连接 ftp.quit() # 使用示例 local_file = 'path/to/your/local/file.txt' remote_file = 'path/to/your/remote/file.txt' host = 'ftp.example.com' user = 'your_username' password = 'your_password' upload_file(local_file, remote_file, host, user, password)
这个示例定义了一个名为upload_file
的函数,它接受本地文件路径、远程文件路径、FTP服务器地址、用户名和密码作为参数。函数连接到FTP服务器,登录并设置被动模式,然后打开本地文件并将其上传到远程服务器。最后,断开连接。
请确保将示例中的变量替换为您自己的值。