ftp_manager.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from ftplib import FTP


class 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)