是的,Python的ftplib库可以用于传输文件夹
import os from ftplib import FTP def upload_folder(local_folder, remote_folder): ftp = FTP('ftp.example.com') ftp.login(user='username', passwd='password') ftp.cwd(remote_folder) for root, dirs, files in os.walk(local_folder): for file in files: local_path = os.path.join(root, file) remote_path = os.path.join(remote_folder, os.path.relpath(local_path, local_folder)) with open(local_path, 'rb') as f: ftp.storbinary(f'STOR {remote_path}', f) ftp.quit() upload_folder('/path/to/local/folder', '/path/to/remote/folder')
这个示例中,upload_folder
函数接受本地文件夹路径和远程文件夹路径作为参数。它首先连接到FTP服务器并登录,然后遍历本地文件夹中的所有文件,并将它们上传到远程文件夹。注意,这个示例仅适用于简单的FTP服务器,对于需要被动模式的FTP服务器,你可能需要使用其他库(如ftplib
的替代品pyftpdlib
)。