FtpUitl.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import ftplib
  2. import os
  3. class FtpOper:
  4. ftp = ftplib.FTP()
  5. def __init__(self):
  6. super().__init__()
  7. def close(self):
  8. self.ftp.quit() # 关闭服务器
  9. # 上传文件
  10. def uploadfile(self, localfile, remotepath):
  11. if not os.path.isfile(localfile):
  12. print('不是文件')
  13. filename = os.path.split(localfile)[-1] # 获取文件名
  14. self.ftp.cwd(remotepath)
  15. with open(localfile, 'rb') as file: # 读取文件
  16. self.ftp.storbinary(f'STOR {filename}', file) # 二进制存储
  17. # 上传文件夹
  18. def uploaddir(self, localdir, remotepath):
  19. # if not os.path.isdir(localdir):
  20. # print('不是文件夹')
  21. dirname = os.path.split(localdir)[-1] # 文件夹名称
  22. new_remotepath = remotepath + '/' + dirname # os.path.join(remotepath, dirname)
  23. self.makedir(dirname, remotepath, new_remotepath)
  24. for localfile in os.listdir(localdir):
  25. src = os.path.join(localdir, localfile)
  26. if os.path.isfile(src):
  27. self.uploadfile(src, new_remotepath)
  28. elif os.path.isdir(src):
  29. self.uploaddir(src, new_remotepath) # 嵌套的文件夹:{src}
  30. self.ftp.cwd('..')
  31. # 创建文件夹
  32. def makedir(self, dirname, remotepath, new_remotepath):
  33. try:
  34. self.ftp.cwd(new_remotepath)
  35. print('文件夹已存在')
  36. return '文件夹已存在'
  37. except ftplib.error_perm:
  38. try:
  39. self.ftp.cwd(remotepath)
  40. self.ftp.mkd(dirname)
  41. print(f'文件夹创建成功:{remotepath}/{dirname}')
  42. return '文件夹创建成功'
  43. except ftplib.error_perm as ex:
  44. print(f'创建文件夹失败:{ex}')
  45. return '创建文件夹失败'
  46. # 下载文件
  47. def downloadfile(self, localfile, remotefile):
  48. remotepath, remotefile_name = os.path.split(remotefile)
  49. if self.is_exist(remotepath, remotefile_name): # 判断文件是否存在
  50. with open(localfile, 'wb') as file:
  51. self.ftp.retrbinary(f'RETR {remotefile}', file.write)
  52. print(f'文件下载成功:{localfile}')
  53. else:
  54. print('文件不存在')
  55. # 下载文件夹
  56. def downloaddir(self, localdir, remotepath):
  57. if not self.is_exist(remotepath):
  58. print('远程文件夹不存在')
  59. else:
  60. if not os.path.exists(localdir):
  61. print(f'创建本地文件夹:{localdir}')
  62. os.makedirs(localdir)
  63. self.ftp.cwd(remotepath)
  64. remotenames = self.ftp.nlst()
  65. for file in remotenames:
  66. localfile = os.path.join(localdir, file)
  67. if file.find('.') == -1: # 此节点下的文件夹
  68. if not os.path.exists(localfile):
  69. os.makedirs(localfile)
  70. self.downloaddir(localfile, remotepath + '/' + file) # 递归文件夹:{remotepath}
  71. else: # 此节点下的文件
  72. self.downloadfile(localfile, file)
  73. self.ftp.cwd('..')
  74. # 判断文件/文件夹是否存在
  75. def is_exist(self, remotepath, filename=None):
  76. try:
  77. self.ftp.cwd(remotepath)
  78. except ftplib.error_perm:
  79. print('远程路径不存在')
  80. return False
  81. if filename is not None:
  82. filelist = self.ftp.nlst()
  83. if filename in filelist:
  84. print(f'存在该文件{filename}')
  85. return True
  86. else:
  87. print(f'没有该文件{filename}')
  88. else:
  89. print(f'远程路径存在{remotepath}')
  90. return True
  91. # 删除文件
  92. def deletfile(self, filename, remotepath):
  93. if self.is_exist(remotepath, filename):
  94. self.ftp.delete(filename)
  95. print('远程文件已删除')
  96. else:
  97. print('远程文件不存在')
  98. # 删除文件夹
  99. def deletedir(self, remotepath):
  100. if self.is_exist(remotepath):
  101. filelist = self.ftp.nlst()
  102. if len(filelist) != 0:
  103. for file in filelist:
  104. new_remotepath = os.path.join(remotepath, file).replace('\\', '/')
  105. if file.find('.') == -1:
  106. self.deletedir(new_remotepath)
  107. else:
  108. self.deletfile(file, remotepath)
  109. self.ftp.rmd(remotepath)
  110. print('远程文件夹删除成功')
  111. else:
  112. print('远程文件夹不存在')
  113. # 重命名
  114. def rename(self, oldname, newname):
  115. try:
  116. self.ftp.rename(oldname, newname)
  117. return True
  118. except ftplib.error_perm:
  119. return False