import os from datetime import datetime import random import string def batch_rename_files(folder_path, random_string_length=0): """ 批量重命名文件,名字改为该文件的创建或修改日期,如果有重名的文件,可以添加一个随机字符串 :param folder_path: 文件夹路径 :param random_string_length: 随机字符串长度,默认为0 """ for filename in os.listdir(folder_path): # 获取文件的创建时间或修改时间 full_path = os.path.join(folder_path, filename) if os.path.isfile(full_path): timestamp = os.path.getmtime(full_path) else: continue # 格式化时间 date_time = datetime.fromtimestamp(timestamp) new_name = date_time.strftime("%Y-%m-%d %H-%M-%S") # 添加随机字符串 if random_string_length > 0: random_string = ''.join(random.choices(string.ascii_lowercase + string.digits, k=random_string_length)) new_name += '-' + random_string # 拼接新的文件名 file_extension = os.path.splitext(filename)[1] new_filename = new_name + file_extension new_full_path = os.path.join(folder_path, new_filename) # 重命名文件 os.rename(full_path, new_full_path) if __name__ == '__main__': folder_path = 'your_folder_path' batch_rename_files(folder_path, random_string_length=5)
时间: 2024-03-16 20:46:09 浏览: 155
Using_the_Date.rar_c# datetime
好的,根据你的要求,将以上代码修改为将修改后的文件命名恢复原来的命名,可以按照以下方式修改:
```
import os
from datetime import datetime
import random
import string
def restore_original_names(folder_path):
"""
批量将文件名恢复为原来的名称,需要在修改文件名之前备份原来的名称
:param folder_path: 文件夹路径
"""
for filename in os.listdir(folder_path):
full_path = os.path.join(folder_path, filename)
if os.path.isfile(full_path):
# 获取备份的文件名
backup_filename = os.path.splitext(filename)[0] + ".bak"
backup_full_path = os.path.join(folder_path, backup_filename)
if os.path.exists(backup_full_path):
# 将备份的文件名恢复为原来的文件名
os.rename(backup_full_path, full_path)
def batch_rename_files(folder_path, random_string_length=0):
"""
批量重命名文件,名字改为该文件的创建或修改日期,如果有重名的文件,可以添加一个随机字符串
:param folder_path: 文件夹路径
:param random_string_length: 随机字符串长度,默认为0
"""
# 先将文件名备份
for filename in os.listdir(folder_path):
full_path = os.path.join(folder_path, filename)
if os.path.isfile(full_path):
# 获取文件的后缀名
file_extension = os.path.splitext(filename)[1]
# 备份原来的文件名
backup_filename = os.path.splitext(filename)[0] + ".bak"
backup_full_path = os.path.join(folder_path, backup_filename)
os.rename(full_path, backup_full_path)
# 批量重命名文件
for filename in os.listdir(folder_path):
full_path = os.path.join(folder_path, filename)
if os.path.isfile(full_path):
# 获取文件的创建时间或修改时间
timestamp = os.path.getmtime(full_path)
# 格式化时间
date_time = datetime.fromtimestamp(timestamp)
new_name = date_time.strftime("%Y-%m-%d %H-%M-%S")
# 添加随机字符串
if random_string_length > 0:
random_string = ''.join(random.choices(string.ascii_lowercase + string.digits, k=random_string_length))
new_name += '-' + random_string
# 拼接新的文件名
new_filename = new_name + file_extension
new_full_path = os.path.join(folder_path, new_filename)
# 重命名文件
os.rename(full_path, new_full_path)
# 将文件名恢复为原来的名称
restore_original_names(folder_path)
if __name__ == '__main__':
folder_path = 'your_folder_path'
batch_rename_files(folder_path, random_string_length=5)
```
这个修改后的代码首先会备份原来的文件名,然后批量重命名所有的文件,并最后将所有文件的名字恢复为原来的名称。
阅读全文