Reputation: 11
I have a hard drive with over 150,000 files. I need to be able to identify files based on their extension and copy them to a new directory while maintaining the directory structure.
I’ve tried this:
srcDir ="c:/folder1/d"
dest ="c:/folder1/G"
os.makedirs(dest)
for root, dirs, files in os.walk(srcDir):
for file in files:
if file[-4:].lower() == '.txt':
shutil.copy(os.path.join(root, file), os.path.join(dest, file))
this saves all the .txt files to c:/arcgis/G instead of maintaining them in their subfolders. Any help is appreciated.
Upvotes: 1
Views: 75
Reputation: 388123
The root
in the walk is not always the same. If it enters a subdirectory, it will contain that subdirectory. As you don’t respect the changed path relatively to your new destination, the hierarchy is not maintained.
What you could do is simply replace the source directory in the root with the destination directory:
for root, dirs, files in os.walk(srcDir):
dRoot = root.replace(srcDir, dest)
os.makedirs(dRoot)
for file in files:
if file[-4:].lower() == '.txt':
shutil.copy(os.path.join(root, file), os.path.join(dRoot, file))
Upvotes: 1