import os
def traverse_dir(path, format=None):
"""
:param path: 文件夹路径
:param format: 要修改成的文件后缀,要加.
:return:None
"""
for root, dirs, files in os.walk(path):
for file in files:
num = 1
file_path = os.path.join(root, file)
filename, _format = os.path.splitext(file_path)
_format = format or _format
new_path = os.path.join(root, "%05d" % num + _format)
while os.path.exists(new_path):
if new_path==file_path:
break
num += 1
new_path = os.path.join(root, "%05d" % num + _format)
else:
os.rename(file_path, new_path)
print(file_path, "变更为-->", new_path)
for dir in dirs:
dir_path = os.path.join(root, dir)
print(dir_path)
traverse_dir(dir_path, format)
if __name__ == '__main__':
path = r"D: est"
traverse_dir(path, ".json")