bluejilo.blogg.se

Python find file in directory with pattern
Python find file in directory with pattern














Search_path=input("Put the directory here:")įor fname in os.

Python find file in directory with pattern code#

I was trying with the following code for this kind of problem, please have a look.

python find file in directory with pattern

Print(bcolors.FAIL + '' + bcolors.ENDC + ' ', fname, ' ', 'does not contain', ' ', search_str, sep="") Print(bcolors.OKGREEN + '' + bcolors.ENDC + ' ', fname, sep="") # If path does not exist, set search path to current directoryįor fname in os.listdir(path=search_path): If not (search_path.endswith("/") or search_path.endswith("\\") ): # Append a directory separator if not already present Search_str = input("Enter the search string : ") Search_path = input("Enter directory path to search : ") You don't need the colors, they are just cool and you may find that you can learn more than one thing in my code :) import os Here is the most simple answer I can give you. Print(findFiles(strings=, dir="C:/Users/User/Desktop/", subDirs=True, fileContent=True, fileExtensions=False))

python find file in directory with pattern

If filename.partition(".") in supportedTypes: '''Returns the content of a file of a supported type (list: supportedTypes)''' If not os.path.isdir(os.path.join(root, f).replace("\\", "/")):įilesInDir.append(os.path.join(root, f).replace("\\", "/"))įilename, extension = os.path.splitext(file)įileText = os.path.basename(filename).lower() If os.path.isfile(os.path.join(dir, filename).replace("\\", "/")):įilesInDir.append(os.path.join(dir, filename).replace("\\", "/"))įor root, subdirs, files in os.walk(dir): # 'fileExtensions': True/False : Look for a specific file extension -> 'fileContent' is ignored # 'fileContent': True/False :Also look for the strings in the file content of every file

python find file in directory with pattern

# 'subDirs': True/False : Look in sub-directories of your folder # Finds all the files in 'dir' that contain one string from 'strings'. import osĭef findFiles(strings, dir, subDirs, fileContent, fileExtensions): The downside is of course that it's quite chunky code. On using the above two functions to find 5076 files matching the regex filename. It is also quite easy to add support for other ones. It comes with the feature of also checking in sub-directories, as well as being able to handle multiple file types.














Python find file in directory with pattern