python真香,我马上放下了手里的C++ boost.h。
存个代码以后参考。
import os
def rename_dfs(cur_path):
file_names = os.listdir(cur_path)
for old_name in file_names:
# print(old_name)
if os.path.isdir(os.path.join(cur_path, old_name)):
rename_dfs(os.path.join(cur_path, old_name))
continue
new_name = old_name
name_list = new_name.split('.')
if name_list.count('in') > 0:
name_list = list(filter(lambda x: x != 'in', name_list))
new_name = '_'.join(name_list) + '.in'
elif name_list.count('out') > 0:
name_list = list(filter(lambda x: x != 'out', name_list))
new_name = '_'.join(name_list) + '.out'
os.renames(os.path.join(cur_path, old_name), os.path.join(cur_path, new_name))
# path为批量文件的文件夹根目录
path = '.\\workspace'
rename_dfs(path)
下面这样写更爽:
import os
import os.path as osp
def ren(r, s, suf):
if name.find(suf) != -1:
t = s.replace(suf, '') + suf
os.renames(osp.join(r, s), osp.join(r, t))
for root, dirs, files in os.walk('.\\workspace'):
for name in files:
ren(root, name, '.in')
ren(root, name, '.out')