chase
chase

Reputation: 3

Search a folder and sub folders for files starting with criteria

I have a folder "c:\test" , the folder "test" contains many sub folders and files (.xml, .wav). I need to search all folders for files in the test folder and all sub-folders, starting with the number 4 and being 7 characters long in it and copy these files to another folder called 'c:\test.copy' using python. any other files need to be ignored.

So far i can copy the files starting with a 4 but not structure to the new folder using the following,

from glob import glob

import os, shutil

root_src_dir = r'C:/test'    #Path of the source directory
root_dst_dir = 'c:/test.copy'  #Path to the destination directory

for file in glob('c:/test/**/4*.*'):
shutil.copy(file, root_dst_dir)

any help would be most welcome

Upvotes: 0

Views: 394

Answers (2)

gmdev
gmdev

Reputation: 3155

This can be done using the os and shutil modules:

import os
import shutil

Firstly, we need to establish the source and destination paths. source should the be the directory you are copying and destination should be the directory you want to copy into.

source = r"/root/path/to/source"
destination = r"/root/path/to/destination"

Next, we have to check if the destination path exists because shutil.copytree() will raise a FileExistsError if the destination path already exists. If it does already exist, we can remove the tree and duplicate it again. You can think of this block of code as simply refreshing the duplicate directory:

if os.path.exists(destination):
    shutil.rmtree(destination)
shutil.copytree(source, destination)

Then, we can use os.walk to recursively navigate the entire directory, including subdirectories:

for path, _, files in os.walk(destination):
    for file in files:
        if not file.startswith("4") and len(os.path.splitext(file)[0]) != 7:
            os.remove(os.path.join(path, file))
    if not os.listdir(path):
        os.rmdir(path)

We then can loop through the files in each directory and check if the file does not meet your condition (starts with "4" and has a length of 7). If it does not meet the condition, we simply remove it from the directory using os.remove.

The final if-statement checks if the directory is now empty. If the directory is empty after removing the files, we simply delete that directory using os.rmdir.

Upvotes: 0

Aplet123
Aplet123

Reputation: 35512

You can use os.walk:

import os
import shutil

root_src_dir = r'C:/test'    #Path of the source directory
root_dst_dir = 'c:/test.copy'  #Path to the destination directory

for root, _, files in os.walk(root_src_dir):
    for file in files:
        if file.startswith("4") and len(file) == 7:
            shutil.copy(os.path.join(root, file), root_dst_dir)

If, by 7 characters, you mean 7 characters without the file extension, then replace len(file) == 7 with len(os.path.splitext(file)[0]) == 7.

Upvotes: 1

Related Questions