Python统计学生健康、形成码

这里写自定义目录标题

  • Python 图片复制到新文件夹
    • PS:

Python 图片复制到新文件夹

帮同学统计

import os
import re
import shutil

all_jkm = os.walk(r'xxx\学生健康码')
all_xcm = os.walk(r'xxx\学生行程码')
all_zwjk = os.walk(r'xxx\自我健康监测')

file_jkm_dir = 'xxx\学生健康码'
file_xcm_dir = 'xxx\学生行程码'
file_zwjk_dir = 'xxx\自我健康监测'
file_jkm = os.listdir(file_jkm_dir)
file_xcm = os.listdir(file_xcm_dir)
file_zwjk = os.listdir(file_zwjk_dir)
new_dir = 'xxx\students' #新的文件夹(包含全班学生), 以学生名字命名一个单独的文件夹,里面有三个图片

studentNames_jkm = []
studentNames_xcm = []
studentNames_zwjk = []

def getStudentsName_jkm() :
    for path, dir, filelist in all_jkm:
        for filename in filelist:
            if filename.endswith(('jpg')) or filename.endswith(('png')):
                fieldname1 = re.split(r'_', filename)
                student = fieldname1[0]
                print(student)
                studentNames_jkm.append(student)
    return studentNames_jkm

def getStudentsName_xcm() :
    for path, dir, filelist in all_xcm:
        for filename in filelist:
            if filename.endswith(('jpg')) or filename.endswith(('png')):
                fieldname1 = re.split(r'_', filename)
                student = fieldname1[0]
                print(student)
                studentNames_xcm.append(student)
    return studentNames_xcm

def getStudentsName_zwjk() :
    for path, dir, filelist in all_zwjk:
        for filename in filelist:
            if filename.endswith(('jpg')) or filename.endswith(('png')):
                fieldname1 = re.split(r'_', filename)
                student = fieldname1[0]
                print(student)
                studentNames_zwjk.append(student)
    return studentNames_zwjk

def add_all():
    studentNames1 = getStudentsName_jkm()
    for img_jkm, img_xcm, img_zwjk, name in zip(file_jkm, file_xcm, file_zwjk, studentNames1):
        # newdir = os.path.join(new_dir, name)
        # print(newdir)
        jkm = name + '_健康码'
        print(jkm)

        xcm = name + '_行程码'
        print(xcm)

        zwjk = name + '_自我健康监测'

        jkm_origin_dir = os.path.join(file_jkm_dir, img_jkm)
        xcm_origin_dir = os.path.join(file_xcm_dir, img_xcm)
        zwjk_origin_dir = os.path.join(file_zwjk_dir, img_zwjk)
        enw2_dir = os.path.join(new_dir, name)

        if os.path.exists(enw2_dir):
            shutil.copy(jkm_origin_dir,
                        os.path.join(enw2_dir, jkm+'.jpg'))
            shutil.copy(xcm_origin_dir,
                        os.path.join(enw2_dir, xcm + '.jpg'))
            shutil.copy(zwjk_origin_dir,
                        os.path.join(enw2_dir, zwjk + '.jpg'))

            # shutil.copy(file_jkm_dir+img_jkm,
            #             new_dir+jkm)

        else:
            os.makedirs(enw2_dir)
            # file_or_name = file_jkm_dir + img_jkm
            # file_new_name = new_dir + jkm
            # print(file_or_name)
            # print(file_new_name)

            shutil.copy(jkm_origin_dir,
                        os.path.join(enw2_dir, jkm+'.jpg'))

            shutil.copy(xcm_origin_dir,
                        os.path.join(enw2_dir, xcm + '.jpg'))

            shutil.copy(zwjk_origin_dir,
                        os.path.join(enw2_dir, zwjk + '.jpg'))

            # shutil.copy(file_jkm_dir + img_jkm,
            #             new_dir + jkm)

def add_jkm():
    student_jkm = getStudentsName_jkm()
    for img_jkm, name in zip(file_jkm, student_jkm):
        # newdir = os.path.join(new_dir, name)
        # print(newdir)
        jkm = name + '_健康码'
        print(jkm)

        jkm_origin_dir = os.path.join(file_jkm_dir, img_jkm)
        enw2_dir = os.path.join(new_dir, name)

        if os.path.exists(enw2_dir):
            shutil.copy(jkm_origin_dir,
                        os.path.join(enw2_dir, jkm + '.jpg'))

        else:
            os.makedirs(enw2_dir)
            shutil.copy(jkm_origin_dir,
                        os.path.join(enw2_dir, jkm + '.jpg'))

def add_xcm():
    student_xcm = getStudentsName_xcm()
    for img_xcm, name in zip(file_xcm, student_xcm):
        # newdir = os.path.join(new_dir, name)
        # print(newdir)
        xcm = name + '_行程码'
        print(xcm)

        xcm_origin_dir = os.path.join(file_xcm_dir, img_xcm)
        enw2_dir = os.path.join(new_dir, name)

        if os.path.exists(enw2_dir):
            shutil.copy(xcm_origin_dir,
                        os.path.join(enw2_dir, xcm + '.jpg'))

        else:
            os.makedirs(enw2_dir)
            shutil.copy(xcm_origin_dir,
                        os.path.join(enw2_dir, xcm + '.jpg'))

def add_zwjk():
    student_zwjk = getStudentsName_zwjk()
    for img_zwjk, name in zip(file_zwjk, student_zwjk):
        # newdir = os.path.join(new_dir, name)
        # print(newdir)
        zwjk = name + '_自我健康监测'
        print(zwjk)

        zwjk_origin_dir = os.path.join(file_zwjk_dir, img_zwjk)
        enw2_dir = os.path.join(new_dir, name)

        if os.path.exists(enw2_dir):
            shutil.copy(zwjk_origin_dir,
                        os.path.join(enw2_dir, zwjk + '.jpg'))

        else:
            os.makedirs(enw2_dir)
            shutil.copy(zwjk_origin_dir,
                        os.path.join(enw2_dir, zwjk + '.jpg'))

add_jkm()
add_xcm()
add_zwjk()

PS:

写得有点冗余,懒得优化了

你可能感兴趣的:(工具使用,python,开发语言,后端)