Update 更新文件名.py
This commit is contained in:
+22
-40
@@ -1,48 +1,30 @@
|
|||||||
import datetime
|
import datetime
|
||||||
import os
|
import os
|
||||||
|
|
||||||
# 记录时间的文件路径
|
# 获取当前日期时间
|
||||||
time_file_path = '上次更新时间.txt'
|
now = datetime.datetime.now()
|
||||||
|
current_date = now.strftime("%m%d")
|
||||||
|
|
||||||
def rename_files():
|
# 获取当前目录下的所有文件
|
||||||
try:
|
all_files = os.listdir(os.getcwd())
|
||||||
# 尝试读取上次记录的时间
|
|
||||||
with open(time_file_path, 'r') as time_file:
|
|
||||||
last_update_time = time_file.read()
|
|
||||||
print(f"Last update time read: {last_update_time}")
|
|
||||||
except FileNotFoundError:
|
|
||||||
last_update_time = ""
|
|
||||||
|
|
||||||
# 获取当前日期时间
|
# 要重命名的文件名列表(初始文件名)
|
||||||
now = datetime.datetime.now()
|
initial_filenames = ['综合源.m3u', '组播优选.txt', '综合源.txt']
|
||||||
current_date = now.strftime("%m%d")
|
|
||||||
|
|
||||||
# 获取当前目录下的所有文件
|
# 删除初始文件名前面带有任意字符的文件
|
||||||
all_files = os.listdir(os.getcwd())
|
for old_filename in all_files:
|
||||||
|
if any(not old_filename.startswith(current_date) and old_filename.endswith(init_filename) for init_filename in initial_filenames):
|
||||||
|
full_old_path = os.path.join(os.getcwd(), old_filename)
|
||||||
|
if os.path.exists(full_old_path):
|
||||||
|
os.remove(full_old_path)
|
||||||
|
print(f"Deleted {old_filename}")
|
||||||
|
|
||||||
# 要重命名的文件名列表(初始文件名)
|
# 重命名初始文件名的文件
|
||||||
initial_filenames = ['综合源.m3u', '组播优选.txt', '综合源.txt']
|
for old_filename in all_files:
|
||||||
|
if any(old_filename.endswith(init_filename) for init_filename in initial_filenames):
|
||||||
|
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)
|
||||||
|
os.rename(full_old_path, full_new_path)
|
||||||
|
print(f"Renamed {old_filename} to {new_filename}")
|
||||||
|
|
||||||
# 重命名初始文件名的文件
|
|
||||||
for old_filename in all_files:
|
|
||||||
if any(old_filename.endswith(init_filename) for init_filename in initial_filenames):
|
|
||||||
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)
|
|
||||||
os.rename(full_old_path, full_new_path)
|
|
||||||
print(f"Renamed {old_filename} to {new_filename}")
|
|
||||||
|
|
||||||
# 删除上一次命名过的文件
|
|
||||||
if last_update_time:
|
|
||||||
for old_filename in all_files:
|
|
||||||
if old_filename.startswith(f"{last_update_time}") and any(old_filename.endswith(init_filename) for init_filename in initial_filenames):
|
|
||||||
full_old_path = os.path.join(os.getcwd(), old_filename)
|
|
||||||
if os.path.exists(full_old_path):
|
|
||||||
os.remove(full_old_path)
|
|
||||||
print(f"Deleted {old_filename}")
|
|
||||||
|
|
||||||
# 更新记录时间的文件
|
|
||||||
with open(time_file_path, 'w') as time_file:
|
|
||||||
time_file.write(current_date)
|
|
||||||
|
|
||||||
rename_files()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user