python3 根据表格给出的信息批量修改文件名

数据:

杂乱无章的数据文件名
&
留学生中心给的选课签字表
在这里插入图片描述

代码:

# -*- coding: utf-8 -*-
"""
Created on Sat Nov  2 17:00:20 2019

@author: BAI Depei
"""

import openpyxl as ox
import os

#依据老师给的最终的成绩打分表,依据学号修改收到的作业的名称(按照表中的顺序)

score_list = ox.load_workbook('2-地球系统科学课程成绩(遥感部分)-2019.xlsx')
sheet = score_list['Worksheet']
#----------------------------获取学号、序号、姓名-------------------------------
student_id = []
for cell in list(sheet.columns)[1]:
    a = cell.value
    student_id.append(a)
order_base = []
for cell in list(sheet.columns)[0]:
    b = cell.value
    order_base.append(b)
name_base = []
for cell in list(sheet.columns)[2]:
    c = cell.value
    name_base.append(c)
#---------------------------获取目录下的所有作业文件----------------------------
file_list = os.listdir('E:\\课程PPT\\earth system science\\assignment2')
ass_file = []
for i in file_list:
    a,b = os.path.splitext(i)
    if b == '.xlsx' or b == '.py' or b == '' :
        continue
    else:
        ass_file.append(i)
#--------------------------修改文件名--------------------------------------
for n in ass_file:
    a,b = os.path.splitext(n)
    index = n.find('201')
    student_id_check = n[index:index+15]
    #学号长度为15
    index_student_id = student_id.index(student_id_check)
    order = order_base[index_student_id]
    name = name_base[index_student_id]
    new_name = str(order) + '_second_' + str(student_id_check) + '_' + name
    old__path_name =  'E:\\课程PPT\earth system science\\assignment2\\'+n
    new_path_name = 'E:\\课程PPT\\earth system science\\assignment2\\新文件名\\' + new_name +b
    os.rename(old__path_name,new_path_name)
    #attention: raname之后原来的文件就不存在了,所以记得提前做好备份.
print('task is successful!')    

结果:

在这里插入图片描述
总共62个人,1号没有在名单上,因为1号还没有交作业。

------------------------------------------------
版权归作者 小白是哪个小白_ 所有,转载、引用请注明链接出处。

发布了19 篇原创文章 · 获赞 28 · 访问量 5059

猜你喜欢

转载自blog.csdn.net/qq_37970770/article/details/102877315
今日推荐