DEV Community

Phil Lambert
Phil Lambert

Posted on

Problem with moving files to subfolders

Hey,
I have a problem with the code below. It should automatically move files with the same name (or only a part of it) as the directory into directories and subdirectories.
It works very well for root directories but not for subdirectories. I thought os.walk was going through the whole tree structure. If you see where the bug comes from.... Thank you for your help.

def moveFiles2Folder():
root_path = lien vers mon répertoire de départ
for (root, dirs, files) in os.walk(root_path):
for dir in dirs :
for file in files :
fileX = os.path.splitext(file)[0]
if fileX in dir :
shutil.copy(os.path.join(root_path,file),os.path.join(root_path, dir))

Top comments (0)