0

I am writing a function that will recursively check all directories in a drive, that part works fine. However, I want to ignore everything in a certain directory, in this case if the directory has "win" or "$win" in it.

Code:

def without_system_files(drive:str, lookup_name:str):
    print(f"Searching... {drive.title()}:\\", end="\r")

    # gets all directories
    for cur_dir, _, _ in os.walk(os.getcwd()):
        # skips to the next directory if it's a windows file
        if "win" in path.lower() or "$win" in path.lower()  # ---HERE---
            continue
        # gets all files
        files = os.listdir(cur_dir)
        # for every file in the dir
        for file in files:
            # make a proper path, if it's the root folder, don't add another \
            if len(cur_dir) != 3:
                path = cur_dir + "\\" + file
            else:
                path = cur_dir + file
            # if the file has the lookup_name (we use file opposed to path because path would return positive if the folder fits the check for lookup_name)
            if lookup_name in file:
                # if path is a file
                if os.path.isfile(path):
                    #found_files = found_files + ("\nFound file at: " + path)
                    print("Found file at: " + path)

The ---HERE--- comment indicates my current solution for this, but it checks every subdir in the dir, I wast it to ignore the entire "tree" of directories if this returns true

Things of note: This is written in python 3.11 This does not meet my requirements, assuming I understand the answers in that thread

Oskar
  • 119
  • 8
  • `os.walk()` gives you lists of the subdirectories and files in the current directory being walked - but you're simply throwing these away. Calling `os.listdir()` is pointless, you had the list of files already. And you are allowed to remove elements from the subdirectories list, to prevent walking into them. – jasonharper Nov 28 '22 at 23:15
  • @jasonharper I didn't realize `os.walk()` returned files and dirs aswell, thank you! – Oskar Nov 29 '22 at 15:48

0 Answers0