[Python] I want to collect specific files scattered in each folder in one place

Introduction

Qiita's first post. I was always indebted to him, so I thought I should publish the finished product.

I want to collect specific files scattered in each folder in one place

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 ..

image.png

Code execution

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

[Python] I want to collect specific files scattered in each folder in one place
I want to do Dunnett's test in Python
I want to create a window in Python
I want to merge nested dicts in Python
How to get the files in the [Python] folder
I want to display the progress in Python!
I want to write in Python! (1) Code format check
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to write in Python! (2) Let's write a test
Even in JavaScript, I want to see Python `range ()`!
I want to randomly sample a file in Python
I want to work with a robot in python.
I want to write in Python! (3) Utilize the mock
I want to use the R dataset in python
I want to do something in Python when I finish
I want to manipulate strings in Kotlin like Python!
I want to write a triple loop and conditional branch in one line in python
I want to do something like sort uniq in Python
I want to be able to run Python in VS Code
I want to make input () a nice complement in python
I want to debug with Python
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
[Python / AWS Lambda layers] I want to reuse only module in AWS Lambda Layers
I want to know the population of each country in the world.
I tried to summarize the contents of each package saved by Python pip in one line
I tried to implement PLSA in Python
I tried to implement permutation in Python
I want to print in a comprehension
I tried to implement PLSA in Python 2
I want to use jar from python
I want to build a Python environment
Create a command to delete all temporary files generated in a specific folder
I want to batch convert the result of "string" .split () in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
I want to analyze logs with Python
I tried to implement ADALINE in Python
Environment maintenance made with Docker (I want to post-process GrADS in Python
I want to color a part of an Excel string in Python
I wanted to solve ABC159 in Python
I made a script in python to convert .md files to Scrapbox format
I tried to implement PPO in Python
I want to embed Matplotlib in PySimpleGUI
I want to do a monkey patch only partially safely in Python
[Python] I want to know the variables in the function when an error occurs!
I want to use Python in the environment of pyenv + pipenv on Windows 10
I want to create a priority queue that can be updated in Python (2.7)
How to drop Google Docs in one folder in a .txt file with python
I want to see a list of WebDAV files in the Requests module
I want to get / execute variables / functions / classes of external files from Python
Python program is slow! I want to speed up! In such a case ...
I want to get the file name, line number, and function name in Python 3.4
Convert FBX files to ASCII <-> BINARY in Python
I want to pin Datetime.now in Django tests
Summary of how to import files in Python 3
I want to memoize including Python keyword arguments
I was able to recurse in Python: lambda
[Python] I want to manage 7DaysToDie from Discord! 1/3
One liner to 100% CPU1 core usage in Python
I want to make a game with Python
I want to visualize csv files using Vega-Lite!