Automatic batch file name change based on python script

Automatic batch file name change based on python script

   As a university monitor, I often face the problem of collecting homework. In order to facilitate the counting of the number of electronic files, and for the sake of neatness and beauty, teachers often specify the naming format of the assignments.
For example: "Class+student ID+name_homework one" format. However, the names of the assignments submitted by the students are often strange.
   There is this: "Communication 170101 + Zhang San + Homework One"
   also has this: "1403170101+ Zhang San_ Homework One"
   and even this: "Communication 170101 Zhang Three Homework One"
clearly and correctly named: "Communication 170101 sheets three_homework one". But it is such a simple problem that many people cannot do well. As a result, I had to rename them one by one when receiving homework, which made my brain bigger.

So I wrote a small program in python to automatically correct all the messy names.

Not much to say, just look at the program. But I have to introduce the preparations first.

First, I have an excel file with a full class list on the desktop. Insert picture description here
After opening it, it
Insert picture description here
looks like this. Then you can put all the files to be renamed into another folder.

import os
import pandas as pd 
import re 

#这个函数是把每个人的学号和名字从excel文件里提取出来,以便下面改名
def excel_to_list(name_list):  
    df = pd.read_excel(name_list, usecols=[1,2],names=None) 
    #读取项目名称列
    #usecols=[1,2]表明取第二列和第三列,也就是学号和名字。
    #names=None表明不要列名
    df_li = df.values.tolist()
    #print(df_li)
    return df_li  #return的就是学号和名字


if __name__ == '__main__':
    name_list = "C:\\Users\\admin\\Desktop\\通信1701名单.xlsx"    #花名册地址
    path = 'C:\\Users\\admin\\Desktop\\新建文件夹\\'              #收作业地址
    work_name = '预习作业一'                                   #作业名字后缀    
    num_name_list = excel_to_list(name_list)
    count = 0; #数数一共有几份作业,看看齐了没


    for file in os.listdir(path): #遍历作业地址中每一份作业
        try:
            type = str(file).split('.')[1]  #判断是.doc文件还是.pdf文件等,保持格式不变
            type = '.' + type
            print("检测到此次作业类型为",type,"类型")
        except:
            type = None  #文件夹就没有格式后缀了      
            print("检测到此次作业类型为文件夹类型")  
        ini_taskname = str(file).split('.')[0] #把文件名中除了格式后缀的部分拿出来
        for nn in num_name_list:
            num = str(nn[0]) #学号
            na = nn[1] #名字
            if re.findall(r'1402',ini_taskname): #正则表达式,如果在学号中找到了‘1402’,说明是电子专业
                class_name = '电子'
            else:
                class_name = '通信'                                                         
            if re.findall(str(na)[0:3],ini_taskname): #只要文件名中写了本人姓名,就成功匹配到他的名字和学号。
                count = count+1;
                if type is  None: #对所有文件夹改名
                    os.rename(os.path.join(path,file),os.path.join(path,class_name+str(num[-6:])+str(na)+'_'+work_name))
                else: #对所有非文件夹文件改名
                    os.rename(os.path.join(path,file),os.path.join(path,class_name+str(num[-6:])+str(na)+'_'+work_name+type))
					#新的文件夹名字为“作业地址+专业名+班级学号(num[-6:0]是班级+学号的意思)+名字+下划线+文件格式”
    print("本次共计处理",count,"个文件") #看看作业收起了没

Thinking about it, there is no good way to match everyone's homework to himself, only to use regular expressions to match names. No one will hand in their homework without writing their own name, right?

I suggest you drop out

In general, this is a very useful script. I was very stupid to manually change the file name for the first two years, and wasted a lot of time. I hope this small code can help you who want to receive files frequently.

If you think it’s not bad, I suggest you like it and share other useful things with you next time.

Guess you like

Origin blog.csdn.net/ddatalent/article/details/109258369
Recommended