From 56ac25a5bfb44436116bb0638cf554c33449aefc Mon Sep 17 00:00:00 2001 From: zhangshu Date: Wed, 28 Aug 2024 20:50:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Esftp=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sftp.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/sftp.py b/sftp.py index 1807930..f00a80f 100644 --- a/sftp.py +++ b/sftp.py @@ -9,9 +9,14 @@ import requests import time import json + +def upload(path, remote): + pass + + if __name__ == "__main__": # 读取配置文件 - with open('search_video_config.json', 'r', encoding='utf-8') as f: + with open('sftp_config.json', 'r', encoding='utf-8') as f: # 使用json.load()方法读取文件内容 data = json.load(f) # 初始化日志 @@ -41,5 +46,50 @@ if __name__ == "__main__": Logger.info("remote_root:{},local_root:{}".format(remote_root, local_root)) # 遍历所有文件,然后上传 - - + region_names = os.listdir(local_root) + for region_name in region_names: + # sftp创建region文件夹 + try: + sftp_client.chdir(remote_root + "/" + region_name) + except BaseException: + Logger.info(f"create region dir: {remote_root}/{region_name}") + sftp_client.mkdir(remote_root + "/" + region_name) + sftp_client.chdir(remote_root + "/" + region_name) + channel_names = os.listdir(f"{local_root}/{region_name}") + for channel_name in channel_names: + # sftp创建channel文件夹 + try: + sftp_client.chdir(f"{remote_root}/{region_name}/{channel_name}") + except BaseException: + Logger.info(f"create channel dir: {remote_root}/{region_name}/{channel_name}") + sftp_client.mkdir(f"{remote_root}/{region_name}/{channel_name}") + sftp_client.chdir(f"{remote_root}/{region_name}/{channel_name}") + # 获取本地文件夹 + srt_names = os.listdir(f"{local_root}/{region_name}/{channel_name}") + for srt_name in srt_names: + # 获取远程文件路径以及本地文件路径 + srt_path = f"{local_root}/{region_name}/{channel_name}/{srt_name}" + remote_path = f"{remote_root}/{region_name}/{channel_name}/{srt_name}" + # 如果远程文件存在,则进行删除 + try: + sftp_client.stat(remote_path) + # 如果文件存在,删除它 + sftp_client.remove(remote_path) + Logger.info("Remote file '{}' deleted.".format(remote_path)) + except FileNotFoundError: + Logger.info("Remote file '{}' not found.".format(remote_path)) + # 上传本地文件 + try: + # 判断本地文件是否存在,存在则上传 + if os.path.exists(srt_path): + Logger.info("本地文件 '{}' 存在,开始上传.".format(srt_path)) + sftp_client.put(srt_path, remote_path, confirm=False) + os.remove(srt_path) + else: + Logger.info("本地文件 '{}' 不存在,无法上传.".format(srt_path)) + except Exception as e: + Logger.info("上传失败 '{}' 文件名长度{}".format( + remote_path, len(remote_path))) + Logger.error(e) + sftp_client.close() + sftp_client = ssh_client.open_sftp() \ No newline at end of file