最近有个需求:有个新生的文件夹,其中的文件内容以考生号命名。现在需要根据新生excel表格,将文件夹里的文件名改成以新生身份证号命名。
具体代码如下:
import os
import xlrd
from xlrd import xldate_as_tuple
data1 = xlrd.open_workbook('2022级新生分班(大专)---定稿.xls')
table = data1.sheets()[0]
rowNum = table.nrows
def scanfile(path):
filelist = os.listdir(path)
for file in filelist:
file_name = os.path.splitext(file)[0]
file_expand = os.path.splitext(file)[1]
for i in range(1, rowNum):
alldata = table.row_values(i)
student_id = alldata[1]
result_id = alldata[5]
if student_id == file_name:
#重命名图片
os.rename(os.path.join(path,file), os.path.join(path,result_id + file_expand))
else:
continue
scanfile('山东')