[Python] Using pathlib to copy files from subdirectories to other subdirectories based on...

Discussão em 'Python' iniciado por Stack, Julho 5, 2025.

  1. Stack

    Stack Membro Participativo

    Directory 1 contains subfolders of student info, each subfolder is named with the following convention

    LASTNAME, FIRSTNAME (STUDENTNUMBER)


    Directory 2 has 6 subfolders that contain .xlsx student grade sheets in them, each of these excel files is named with the following convention

    LASTNAME, FIRSTNAME (STUDENTNUMBER) marking sheet.xlsx


    I'd like to use pathlib to take the names of the subfolders in directory 1 and find the matching grading sheet in the subfolders in directory 2.

    For example:

    import pathlib as pl

    dir1 = pl.WindowsPath(r'C:\Users\username\directory_1')
    dir2 = pl.WindowsPath(r'C:\Users\username\directory_2')

    for (subfolder, file) in zip(dir1.iterdir(), dir2.rglob("*.xlsx")):
    if str.lower(subfolder.name) is in str.lower(file.name): #I run up against a wall here
    copy file into subfolder
    print(f'{file.name} copied to {subfolder.name}')


    Apologies if this question is unclear but any assistance would be appreciated. I have also tried to implement ideas from this answer but I'm not skilled enough with python to modify it for my needs.

    Continue reading...

Compartilhe esta Página