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 impliment ideas from this answer but I'm not skilled enough with python to modify it for my needs.
This is untested, but I think what you want to be doing is creating the potential file name from the subfolders in directory 1, using that to search in directory 2 and then moving files that you find.
from pathlib import Path
from shutil import copy
dir1 = Path("C:\Users\username\directory_1")
dir2 = Path("C:\Users\username\directory_2")
for folder in dir1.iterdir():
# We're only interested in folders
if not folder.is_dir():
continue
target_file = f"{folder.name} marking sheet.xlsx"
for file in dir2.rglob(target_file):
# copy(file, dest)
I'm not sure about where you want the files to be copied to, but you can set the dest
variable for each subfolder of dir1
or result of the rglob
. Another thing to note, you may find multiple files with the target name in different directories so I would caution against copying them all to the same place!