[python]代码库
import os
def add_prefix_suffix_to_files(directory, prefix='', suffix=''):
# 获取目录中的所有文件
for filename in os.listdir(directory):
# 获取文件扩展名
file_ext = os.path.splitext(filename)[1]
# 构建新的文件名
new_name = f"{prefix}{filename}{suffix}{file_ext}"
# 完整的旧文件路径和新文件路径
old_file = os.path.join(directory, filename)
new_file = os.path.join(directory, new_name)
# 重命名文件
os.rename(old_file, new_file)
print(f'Renamed: {old_file} to {new_file}')
# 使用示例
directory_path = '/path/to/your/directory' # 替换为你的目录路径
prefix = 'prefix_' # 自定义前缀
suffix = '_suffix' # 自定义后缀
add_prefix_suffix_to_files(directory_path, prefix, suffix)