根据姓名读取学号并重命名
import xlrd
import xlwt
import os
path='.\\'
data = xlrd.open_workbook('data.xlsx')
table = data.sheet_by_name(data.sheet_names()[0])
for file in os.listdir(path):
#判断是否是文件
if os.path.isfile(os.path.join(path,file))==True:
if '.doc' in file and '_222' not in file:
for i in range(1,len(table.col_values(0))):
if table.col_values(2)[i] in file:
num = str(table.col_values(1)[i])[:-2]
break
else:
num = '0'
if num != '0':
temp = file.split('.')
new_name = temp[0] + '_' + num + '.' + temp[1]
os.rename(os.path.join(path,file),os.path.join(path,new_name))