"""
@Time : 2018/6/28
@Author : LiuXueWen
@Site :
@File : ChangeFileName.py
@Software: PyCharm
@Description:
"""
import os
class changefilename():
'''
path: 指定需要修改文件名称的路径
str:指定修改的位置上需要替换的字符串内容
index:指定需要修改的位置(下标)
'''
def __init__(self,path,str,index):
self.path = path
self.str = str
self.index = index
'''
name_split:指定文件名称拆分的规则
'''
def changename(self,name_split):
for file_list in os.listdir(self.path):
file_name_split = file_list.split(name_split)
file_name_split[self.index] = self.str
for i in range(len(file_name_split)):
new_file_name = "_".join(file_name_split)
print new_file_name
os.rename(os.path.join(self.path,file_list),os.path.join(self.path,new_file_name))
if __name__ == '__main__':
changefilename("C:\Users\Carol\Desktop\8","20180627",3).changename("_")