收作業小工具(工具)

2020-10-13 12:00:15

平臺:

64位元系統

說明:

全班的作業在一個資料夾中,標準化命名後統計出誰沒交作業

所用庫:

#有幾個用不上
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from lxml import etree
from tkinter import *
import requests
import random
import pyautogui
import pyperclip
import re
import os
import time
from PIL import Image

任務分解:

1.刪除學號相同的檔案(刪除修改時間靠前的)

2.重新命名,統一格式

3.生成沒交的名單

程式碼:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from lxml import etree
from tkinter import *
import requests
import random
import pyautogui
import pyperclip
import re
import os
import time
from PIL import Image

path = r''   #執行程式前,記得修改主資料夾路徑!
text=""
var=1
Sno={}
Sno_18={'191530101':'張三','191530102':'張四'}#以字典的形式存入學生資訊
def no_data():
    re_no = set()
    os.chdir(path)
    old_names = os.listdir( path )  #取路徑下的檔名,生成列表
    for f_name in old_names:   #遍歷列表下的檔名
        for s_name in old_names:   #遍歷列表下的檔名
            if f_name.split('-')[0] == s_name.split('-')[0]:
                f_time = os.stat(os.path.abspath(f_name)).st_mtime
                s_time = os.stat(os.path.abspath(s_name)).st_mtime
                if s_time < f_time:
                    re_no.add(s_name)
    for i in re_no:
        os.remove(i)

def rename_subfolders():  
    old_names = os.listdir( path )  #取路徑下的檔名,生成列表
    for old_name in old_names:   #遍歷列表下的檔名
        if old_name.split('.')[1] == 'docx'  or old_name.split('.')[1] == 'doc' :  #防止指令碼檔案放在path路徑下時,被一起重新命名
            tail = old_name.split('.')[1]
            sno=old_name.split('-')[0] 
            new_name = sno + '-' + Sno[sno] + '-' +text + '.' + tail
            os.rename(os.path.join(path,old_name),os.path.join(path,new_name))  #子資料夾重新命名

def no_summit_stu():
    old_names = os.listdir( path )  #取路徑下的檔名,生成列表
    for old_name in old_names:   #遍歷列表下的檔名
        if old_name.split('.')[1] == 'docx'  or old_name.split('.')[1] == 'doc' : 
            sno=old_name.split('-')[0]
            Sno.pop(sno)
    f = open(path +'/not_summit.txt','w',encoding='utf-8')
    for k,v in Sno.items():			# 遍歷字典中的鍵值
        s2 = str(v)                 
        f.write(k+'\t')             
        f.write(s2+'\n')

def window():
    window = Tk()
    window.title('rename')
    window.geometry('1000x500') 
    l = Label(window, text='修改檔名&&統計名單', bg='green', font=('Arial', 12), width=30, height=2)
    # 說明: bg為背景,font為字型,width為長,height為高,這裡的長和高是字元的長和高
    l.pack() 
    var_usr_name = StringVar()
    Label(window, text='path:',font=('Arial', 14)).place(x=300, y=100)
    Entry(window, textvariable=var_usr_name,  font=('Arial', 14)).place(x=375,y=100)
    var_usr_id = StringVar()
    Label(window, text='suffix:',font=('Arial', 14)).place(x=300, y=150)
    Entry(window, textvariable=var_usr_id,  font=('Arial', 14)).place(x=375,y=150)
    global var
    var  =  IntVar()
    var.set(1)
    def r1():
        global var
        var =1
    def r2():
        global var
        var =0
    r1 = Radiobutton(window, text='不改字尾', variable=var, value=1,font=('Arial', 14),command=r1).place(x=500,y=200)
    r2 = Radiobutton(window, text='改字尾', variable=var, value=0,font=('Arial', 14),command=r2).place(x=375,y=200)
    def hit_me():
        global Sno,Sno_18
        Sno=Sno_18.copy()
        global path
        path = var_usr_name.get()
        no_data()
        if var==0:
            print('OK')
            global text
            text = var_usr_id.get()
            rename_subfolders()
        no_summit_stu()
    b = Button(window, text='確定', font=('Arial', 12), width=10, height=1, command=hit_me).place(x=436,y=250)
    window.mainloop()

if __name__ == '__main__': 
    window()

轉EXE:

pyinstaller -F 絕對路徑 -w

缺陷:

只適用於自己班,因為學生資訊是自己班的

總結:


實測好用,免去檢查了。有建議歡迎表達。

QQ:1071949367,交個朋友!