File : fixname.py (直接右键另存为下载)
Type : python
Brief : 批量修必特定类型文件名。
import os
root_dir = "new"
name_prefix = "fix"
def fixname(root_dir, prefix):
"""
Find .txt files and then rename them.
"""
root = os.path.abspath(root_dir)
txt_files = list(
filter(lambda files : os.path.splitext(files)[1] == '.txt',
map(lambda files : os.path.join(root, files),
os.listdir(root))))
cnt = 1
new_txt_files = []
for k in range(len(txt_files)):
new_txt_files.append(os.path.join(root, prefix + str(cnt)) + '.txt')
os.rename(txt_files[k], new_txt_files[k])
cnt += 1
return [txt_files, new_txt_files]
if __name__ == "__main__":
[txt_files, new_txt_files] = fixname(root_dir, name_prefix)
print("Rename complete:")
for k in range(len(txt_files)):
print(txt_files[k] + " => " + new_txt_files[k])
os.system('pause')
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 [ yehuohan@gmail.com ]