屏蔽删除文件代码

This commit is contained in:
胡尧
2025-04-27 15:24:28 +08:00
parent 57789dc5a5
commit 404c56e134

View File

@@ -312,37 +312,37 @@ def transfer_files(
logging.info(f"已传输文件: {item}") logging.info(f"已传输文件: {item}")
# 清空目标目录下的所有内容 # 清空目标目录下的所有内容
try: # try:
target_ftp.ftp.cwd(target_dir) # target_ftp.ftp.cwd(target_dir)
files = target_ftp.ftp.nlst() # files = target_ftp.ftp.nlst()
for f in files: # for f in files:
try: # try:
# 尝试删除文件 # # 尝试删除文件
target_ftp.ftp.delete(f) # target_ftp.ftp.delete(f)
except: # except:
try: # try:
# 如果删除失败,可能是目录,递归删除目录 # # 如果删除失败,可能是目录,递归删除目录
def remove_dir(path): # def remove_dir(path):
target_ftp.ftp.cwd(path) # target_ftp.ftp.cwd(path)
sub_files = target_ftp.ftp.nlst() # sub_files = target_ftp.ftp.nlst()
for sf in sub_files: # for sf in sub_files:
try: # try:
target_ftp.ftp.delete(sf) # target_ftp.ftp.delete(sf)
except: # except:
remove_dir(f"{path}/{sf}") # remove_dir(f"{path}/{sf}")
target_ftp.ftp.cwd('..') # target_ftp.ftp.cwd('..')
target_ftp.ftp.rmd(path) # target_ftp.ftp.rmd(path)
remove_dir(f"{target_dir}/{f}") # remove_dir(f"{target_dir}/{f}")
except: # except:
logging.error(f"无法删除 {f}") # logging.error(f"无法删除 {f}")
pass # pass
logging.info(f"已清空目标目录 {target_dir}") # logging.info(f"已清空目标目录 {target_dir}")
except Exception as e: # except Exception as e:
logging.error(f"清空目标目录失败: {str(e)}") # logging.error(f"清空目标目录失败: {str(e)}")
raise Exception(f"清空目标目录失败: {str(e)}") # raise Exception(f"清空目标目录失败: {str(e)}")
# 开始遍历 # 开始遍历
traverse_dir(source_dir) traverse_dir(source_dir)