pu369com

python批量修改文件名(对各单位上报文件名加上序号并统计未上报的单位)

需求:有几十个文件(不超过100个),要根据文件名加序号

思路:用字典将关键字对应序号,再批量改文件名,代码比较简单:

import os,copy

path = input("请输入包含各单位上报资料的文件夹全路径: ")
units = {"总裁办":"01","办公室":"01","生产处":"02","安全处":"03"}
res = copy.deepcopy(units)
files = os.listdir(path)
for file in files:    
    for unit in units:        
        if unit in file:
            code = units[unit]            
            if file[0:2] == code:                
                res = {k: v for k, v in res.items() if v != code}
            else:
                os.rename(f'{path}/{file}',f'{path}/{code}_{file}')
            if unit in res:
                del res[unit]
print("剩余:",res)
a=input("press any key to exit")

 

posted on 2023-11-23 17:21  pu369com  阅读(22)  评论(0编辑  收藏  举报

导航