Scanning subdirectories for files that match certain filenames

Question:

I want to scan a directory and all its subdirectories for certain file names (i.e. all files having an .log extension, as well as all files whose names are example1.txt or example2.txt), so I can process them further. I succeded in fetching all files ending with .log:

import re
from pathlib import Path

filenames = ["*.log", "example1.txt", "example2.txt"]

input_path = Path("./testfolder")
ls = [p for p in input_path.glob("**/*.log") if p.is_file()]
print(", ".join(str(p) for p in ls))

What do I have to do to get all files having an .log extension, as well as all files whose names are example1.txt or example2.txt?

Asked By: Madamadam

||

Answers:

To scan a directory and its subdirectories for files with specific names, you can use the glob method from the pathlib module and specify the file names you are looking for using wildcard patterns.

import re
from pathlib import Path

# Define the file names you are looking for
filenames = ["*.log", "example1.txt", "example2.txt"]

# Define the input directory
input_path = Path("./testfolder")

# Use the glob method to search for files with the specified names
files = [p for name in filenames for p in input_path.glob("**/{}".format(name)) if p.is_file()]

# Print the list of matching files
print(", ".join(str(p) for p in files))

in this code, the glob method is called once for each file name in the filenames list. This method searches for files with the specified name in the input_path directory and its subdirectories. The resulting list of files is then concatenated into a single list and printed.

Answered By: Flighty
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.