使用python给文件批量命名
首先需要导入os 和xlrd
import os
import xlrd
然后需要定义文件的路径
dirpath = 'C:\\Users\\SGX\Desktop\\PYthon\\sgx'
datapath = 'C:\\Users\\SGX\Desktop\\PYthon\\0127.xlsx'
x1 = xlrd.open_workbook(datapath)#读取excel
sheet1 = x1.sheet_by_name("Sheet1")#读取sheet1
idlist_1 = sheet1.col_values(0)#存放第一列
idlist_2 = sheet1.col_values(1)#存放第二列
idlist_3 = sheet1.col_values(2)#存放第三列
file_names = os.listdir(dirpath)#获取文件夹下所有文件
for id_1 in file_names:
if id_1 in idlist_1:
id_2 = idlist_2[idlist_1.index(id_1)]
id_3 = idlist_3[idlist_1.index(id_1)]
os.renames(os.path.join(dirpath, id_1), os.path.join(dirpath, str(id_2) +'-' +str(id_3) + '.sdf'))#重命名