Search code examples
python-3.xglobpython-os

Iterate through folder/sub-directories and move found regex files into new folder


I´ve got a folder/sub-directories structure as follow:

-main_folder
    -sub_1
         322.txt
         024.ops
    -sub_2
         977.txt
         004.txt
    -sub_3
         396.xml
         059.ops

I´m trying to iterate with os.walk through the folder and its sub-directories and collect the names inside these folders. When a name gets found by a regex rule, I want to either store the path in list or directly move that file into a new folder (mkdir).

I´ve already got the regex done to find the document I want. For example:

find_000_099 = r'\b(0\d{2}.\w{1,4})'
find_300_399 = r'\b(3\d{2}.\w{1,4})'
find_900_999 = r'\b(9\d{2}.\w{1,4})'

I wish my expected result to be like:

-main_folder
    -sub_from_000_099
         024.ops
         004.txt
         059.ops
    -sub_from_300_399
         322.txt
         396.xml
    -sub_from_900_999
         977.txt

Solution

  • Here is a simpler way, using pathlib and shutil

    import re
    import shutil
    from pathlib import Path
    
    new_path = Path("new_folder")
    if not new_path.exists(): new_path.mkdir()
    
    # Getting all files in the main directory
    files = Path("main_folder").rglob("*.*")
    
    regs = {
        r'\b(0\d{2}.\w{1,4})': "sub_1", # find_000_099
        r'\b(3\d{2}.\w{1,4})': "sub_2", # find_300_399
        r'\b(9\d{2}.\w{1,4})': "sub_3"  # find_900_999
    }
    
    for f in files:
        for reg in regs:
            if re.search(reg, f.name):
                temp_path = new_path / regs[reg]
                if not temp_path.exists(): temp_path.mkdir()
    
                # Change the following method to 'move' after testing it
                shutil.copy(f, temp_path / f.name)
                break