ftp_manager.py 123456789101112131415161718192021222324252627282930313233343536373839404142434445from ftplib import FTPclass FtpManager: host = "" username = "" password = "" def __init__(self): """连接到 FTP 服务器""" self.ftp = FTP(self.host) # 替换为你的 FTP 服务器地址 self.ftp.login(user=self.username, passwd=self.password) # 替换为你的用户名和密码 def list_files(self): """列举目录中的所有文件""" files = self.ftp.nlst() return list(files) def upload_file(self, local_file, remote_file): """上传文件""" with open(local_file, 'rb') as f: self.ftp.storbinary(f'STOR {remote_file}', f) print(f'Uploaded: {local_file} to {remote_file}') def download_file(self, remote_file, local_file): """下载文件""" with open(local_file, 'wb') as f: self.ftp.retrbinary(f'RETR {remote_file}', f.write) print(f'Downloaded: {remote_file} to {local_file}') def delete_file(self, remote_file): """删除文件""" self.ftp.delete(remote_file) print(f'Deleted: {remote_file}')if __name__ == "__main__": ftp_manager = FtpManager() __name__files = ftp_manager.list_files() print(__name__files)