如何比较目录中的每个项目与另一个目录中的每个项目?

时间:2019-04-28 10:01:57

标签: python

任务是将学生的家庭作业SQL文件与导师SQL文件进行比较。

我写了两个函数,它们返回一个二维数组(第一个元素是绝对路径,第二个是相对路径)。然后,如果这些值相等,我将比较学生和导师的相对路径,并执行SQL文件(使用绝对路径查找)

还有更优雅的实现吗?

指导者目录的文件夹结构: Homework (folder) ├ 1 (folder) | ├ 1.sql | ├ 2.sql | └ n.sql ├ 2 (folder) | ├ 1.sql | ├ 2.sql | └ n.sql ├ n (folder) | ├ 1.sql | ├ 2.sql | └ n.sql

学生目录的文件夹结构: ├Students Homework (folder) ├Student1(folder) ├ 1 (folder) | ├ 1.sql | ├ 2.sql | └ n.sql ├ 2 (folder) | ├ 1.sql | ├ 2.sql | └ n.sql ├ n (folder) | ├ 1.sql | ├ 2.sql | └ n.sql ├Student2(folder) ├ 1 (folder) | ├ 1.sql | ├ 2.sql | └ n.sql ├ 2 (folder) | ├ 1.sql | ├ 2.sql | └ n.sql ├ n (folder) | ├ 1.sql | ├ 2.sql | └ n.sql

“导师”功能:

def find_mentors_sql(config):

    mentors_sql_abs = []
    mentors_sql_rel = []

    for dirpath, subdirs, files in walk(config["MAIN_DIR"] + '\\Homework'):
        mentors_sql_abs.extend(path.join(dirpath, x) for x in files if x.endswith(".sql"))
        mentors_sql_rel.extend(path.join(path.basename(dirpath), x) for x in files if x.endswith(".sql"))

    mentors_sql = [[0] * 2 for i in range(len(mentors_sql_abs))]

    iter = 0
    for _ in mentors_sql_abs:
        mentors_sql[iter][0] = mentors_sql_abs[iter]
        iter += 1

    iter1 = 0
    for _ in mentors_sql_rel:
        mentors_sql[iter1][1] = mentors_sql_rel[iter1]
        iter1 += 1

    return mentors_sql

“学生”功能(逻辑与上一个相似:

def find_students_sql(config):

    students_sql_abs = []
    students_sql_rel = []

    for dirpath, subdirs, files in walk(config["MAIN_DIR"] + '\\Students Homework'):
        students_sql_abs.extend(path.join(dirpath, x) for x in files if x.endswith(".sql"))
        students_sql_rel.extend(path.join(path.basename(dirpath), x) for x in files if x.endswith(".sql"))

    students_sql = [[0] * 2 for i in range(len(students_sql_abs))]

    iter = 0
    for _ in students_sql:
        students_sql[iter][0] = students_sql_abs[iter]
        iter += 1

    iter1 = 0
    for _ in students_sql:
        students_sql[iter1][1] = students_sql_rel[iter1]
        iter1 += 1

    return students_sql

0 个答案:

没有答案