python获取文件目录下文件名并修改名称

  1、指定对应文件路径,获取目录下所有的文件名,模糊查找文件名后并修改文件名称。

import os

def os_rename(filenum):
    path = 'file/'
    files = os.listdir(path)
    for num in filenum:
        for f in files:
            if num in f and f.endswith('.json'):
                old_name = 'fcc/{}'.format(f)
                new_name = 'fcc/{}.json'.format(num)
                try:
                    os.rename(old_name,new_name)
                except:
                    print("文件名替换失败")

filenum = []
os_rename(filenum)

 

posted @ 2021-03-30 16:52  涛¥哥  阅读(648)  评论(0编辑  收藏  举报