#coding:cp936 __author__ = 'JYC103' import os,re file_dir=r'D:\BaiduYunDownload\從零開始學習linux安全加固\視頻\視頻' file_match=[] old='CDN' new='cdn' os.chdir(file_dir) for file_list in os.listdir(file_dir): #print file_list if file_list.find(old) != -1: file_match.append(os.path.join(file_dir,file_list)) print file_list for item in file_match: print item if item.find(old) !=1: new_file_name=item.replace(old,new) new_file=os.path.join(file_dir,new_file_name) print new_file os.rename(item,new_file)
結果
C:\Python27\python.exe E:/python_ex_eyu/0915/dir_rename.py 第十九課使用CDN加速網站的同時保護網站.exe D:\BaiduYunDownload\從零開始學習linux安全加固\視頻\視頻\第十九課使用CDN加速網站的同時保護網站.exe D:\BaiduYunDownload\從零開始學習linux安全加固\視頻\視頻\第十九課使用cdn加速網站的同時保護網站.exe