Qiita's first post. I was always indebted to him, so I thought I should publish the finished product.
Doesn't this happen often? All Folders have the same folder structure and I want to collect the files sleeping in the sub-sub-folder (this time it is the mdb file) in one place ..
I will forget it every time and check it, so I will leave it below. The environment was run on a Jupyter notebook.
mdb_collect.py
import os
import shutil
import pandas as pd
#Specify the directory you want to save
file_to = r"the location where you want to save them"
#Specify the top of the folder structure to be found from now on
k = os.path.exists(r"Parent folder where you seek the files")
if k==True: #For the time being, check if the folder exists.
root = r"the location where you seek the files"
for folder, subfolders, files in os.walk(root):
# "sub-sub-folder-1"If you find a folder named
#Find and get the mdb file.
if "\sub-sub-folder-1" in folder:
for file in files:
if ".mdb" in file:
file_from = folder + "\\" + file
print(file_from)
shutil.copyfile(file_from,file_to+"\\"+file)
os.walk () walks through all the hierarchies under Parent Folder. Convenient.
Recommended Posts