Python 批量修改目录下的所有文件名
Python 批量修改目录下的所有文件名
主要是使用os库的功能,对文件进行操作
import os
if __name__ == '__main__':
# 设置当前目录为基本目录
case_path = os.path.abspath('.')
# 递归查找case_path所有子目录以及文件,topdown=False由最里层往外查找
for _dir, node_dir, list_file in os.walk(case_path, topdown=False):
# 如果没有子目录
if not node_dir:
# 遍历所有的文件
for file in list_file:
# 文件绝对路径
abs_file = '{}/{}'.format(_dir, file)
if 'xihuan' in abs_file:
new_file = file.replace('xihuan', 'love')
# 把文件名xihuan替换为love
os.rename(file, new_file)

浙公网安备 33010602011771号