Feb-08-2022, 06:50 AM
Not homework, but a very elementary script, that has a seemingly complex problem.
It mostly works. It is used to move directories based on the filetype content. It should move the target filetype *AND* all other types in the dir and subdirs. It mostly works, except when it doesnt.
I can move an App dir with mixed file types 4-5 levels deep mostly without errors. But sometimes only parts of a directory are moved, and sometimes they are moved to dest while a .\dest\dest\sourcedir is created with other portions of the top level source dir.
I believe there is something simple and elementary I am missing here:
#!python38
It mostly works. It is used to move directories based on the filetype content. It should move the target filetype *AND* all other types in the dir and subdirs. It mostly works, except when it doesnt.
I can move an App dir with mixed file types 4-5 levels deep mostly without errors. But sometimes only parts of a directory are moved, and sometimes they are moved to dest while a .\dest\dest\sourcedir is created with other portions of the top level source dir.
I believe there is something simple and elementary I am missing here:
#!python38
import os from fnmatch import fnmatch import shutil #zzmoveapps.py - Script to segregate Application dirs from other types (videos,pics,etexts) root = '.' dest = '.\zApps' for path, subdirs, files in os.walk(root): for name in files: if name.endswith(('.exe', '.zip', '.7z', '.msi','.rar')): print(path) dest2 = os.path.join(dest, path) print(dest2) try: shutil.move(path,dest2) print("Directory '%s' created successfully" % dest2) except OSError as error: print("Directory '%s' can not be created" % dest2)