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)