From 1873e0e66a30e19f72b7235072f118bb9d1c2442 Mon Sep 17 00:00:00 2001 From: frxz751113 <156018267+frxz751113@users.noreply.github.com> Date: Sun, 15 Sep 2024 10:35:42 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E6=9B=B4=E6=96=B0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=90=8D.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- py/更新文件名.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/py/更新文件名.py b/py/更新文件名.py index 42fc6ae..50b6fe9 100644 --- a/py/更新文件名.py +++ b/py/更新文件名.py @@ -21,26 +21,24 @@ def rename_files(): all_files = os.listdir(os.getcwd()) # 要重命名的文件名列表(初始文件名) - initial_filenames = ['综合源.m3u', '组播优选.txt', '综合源.txt'] #, '综合源.m3u', '组播优选.txt', '网络收集.txt' + initial_filenames = ['综合源.m3u', '组播优选.txt', '综合源.txt'] for old_filename in all_files: # 检查文件是否是需要重命名的文件(根据初始文件名判断) if any(old_filename.endswith(init_filename) for init_filename in initial_filenames): - if last_update_time and old_filename.startswith(f"{last_update_time}"): - # 如果只有上次重命名过的文件,用当前时间替换上次时间进行重命名 - original_filename_after_last_time = old_filename[len(last_update_time):] - new_filename = f"{current_date}{original_filename_after_last_time}" - full_old_path = os.path.join(os.getcwd(), old_filename) - full_new_path = os.path.join(os.getcwd(), new_filename) - os.rename(full_old_path, full_new_path) - print(f"Renamed {old_filename} to {new_filename}") - else: + renamed_exists = False + for f in all_files: + if f.startswith(f"{last_update_time}{old_filename}"): + renamed_exists = True + break + if renamed_exists: # 如果同时存在初始文件名和重命名过的文件,删除重命名过的文件 - if any(f.endswith(old_filename) and f.startswith(last_update_time) for f in all_files): - full_old_path = os.path.join(os.getcwd(), f"{last_update_time}{old_filename}") + full_old_path = os.path.join(os.getcwd(), f"{last_update_time}{old_filename}") + if os.path.exists(full_old_path): os.remove(full_old_path) print(f"Deleted {f'{last_update_time}{old_filename}'}") - # 对初始文件名文件进行重命名 + else: + # 如果只有初始文件名的文件,进行重命名 new_filename = f"{current_date}{old_filename}" full_old_path = os.path.join(os.getcwd(), old_filename) full_new_path = os.path.join(os.getcwd(), new_filename)