用Python查看文件夹内文件是否与名单内相同

我现在是一个大学生,这是我在做学委是收电子版作业,需要查人数烦了写的一个查作业的python程序很简单,在这里存储一下,各位大佬路过一笑就好,有问题的话望请斧正。

from tkinter import *
from tkinter import filedialog
import tkinter.messagebox
import tkinter as tk

def main():
    def selectExcelfile1():
        global mdps
        mdps = filedialog.askopenfilename(title="选择TXT文件")#'选择Excel文件', filetypes=[('Excel', '*.xlsx'), ('All Files', '*')])
        print(mdps)
        text1.insert(INSERT, mdps)

    def selectExcelfile2():#获得待处理文件位置
        global wjps
        wjps = filedialog.askdirectory()
        print(wjps)
        text2.insert(INSERT, wjps)

    def selectExcelfile3():#获得结果位置
        global wmdps
        wmdps = filedialog.askdirectory()
        print(wmdps)
        text3.insert(INSERT, wmdps)
        print(wmdps)

    def closeThisWindow():
        root.destroy()

    def doProcess():
        tkinter.messagebox.showinfo('提示', '处理txt文件的示例程序。')
        deal(wjps, mdps, wmdps)

    def deal(wjps, mdps, wmdps):
        md = []
        import os  # 输入输出的模块以下用于把文件中的所有文件文件名变成一个列表,便于遍历检索目标
        caselist = os.listdir(wjps)  # OS.listdir是列出目标路径的文件夹以及文件
        all_name = ""  # 空的字符串
        for a in caselist:
            all_name += a
        print(all_name)  # 此时字符串为所有文件的名
        data = []
        with open(mdps, 'r', encoding='utf-8') as f:  # w是创建新文件,r是只读取文件,wb以二进制方式创建新文件,一般是存储图片等文件。utf-8是字符串的操作格式
            for line in f.readlines():  # readline() 和 .readlines() 之间的差异是后者一次读取整个文件,前者一次一行
                line = line.strip()  # strip代表换行。因为在文本中每行开头都有个"/去掉文本中句子开头与结尾的符号的.split() 这样就把每行的每个字符一个个分开。
                data.append(line)
        print(data)  # 检索目标 返回没有找到的文件(人名)
        rs=0
        for i in range(0, 40):
            if all_name.find(data[i]) + 1:
                continue
            else:
                print(data[i])
                rs+=1
                md.append(data[i])
        print("共有",rs,"未交")
        rs1=["共有",str(rs),"人未交"]
        for i in range(0,3):
            md.append(rs1[i])
        print(md)
        file = open(wmdps + "\\未完成.txt", 'w', )  # 创建了一个文件并“.txt"规定文件类型,并打开文件
        file.write(" ".join(md))  # 写入文件
        file.close()  # 关闭文件

    # 初始化
    root = Tk()

    # 设置窗体标题
    root.title('检索文件')

    # 设置窗口大小和位置
    root.geometry('600x300+570+200')

    label1 = Label(root, text='请选择名单文件:')
    label2 = Label(root, text='请选择文件夹:')
    label3 = Label(root, text='请选择存放结果的文件夹:')
    text1 = Entry(root, bg='white', width=45)
    text2 = Entry(root, bg='white', width=45)
    text3 = Entry(root, bg='white', width=45)
    button1 = Button(root, text='浏览', width=8, command=selectExcelfile1)
    button4 = Button(root, text='浏览', width=8, command=selectExcelfile2)
    button5 = Button(root, text='浏览', width=8, command=selectExcelfile3)
    button2 = Button(root, text='处理', width=8, command=doProcess)
    button3 = Button(root, text='退出', width=8, command=closeThisWindow)

    label1.pack()
    label2.pack()
    label3.pack()
    text1.pack()
    text2.pack()
    text3.pack()
    button1.pack()
    button2.pack()
    button3.pack()
    button4.pack()
    button5.pack()

    label1.place(x=30, y=30)
    label2.place(x=30, y=60)
    label3.place(x=30, y=90)
    text1.place(x=150, y=30)
    text2.place(x=150, y=60)
    text3.place(x=150, y=90)
    button1.place(x=450, y=26)
    button4.place(x=450, y=56)
    button5.place(x=450, y=86)
    button2.place(x=160, y=150)
    button3.place(x=260, y=150)

    root.mainloop()


if __name__ == "__main__":
    main()

这个文件需要读取TXT格式的名单,如果需要用xlsx的文件需要安装一个文件然后稍微改一下程序,如果有同学要用直接拿走吧,我当时懒得下载了就没用,而且这样复用性我觉得可能更好。嘻嘻嘻。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值