Skip to content Skip to sidebar Skip to footer

Python 3.5 Os.walk For Selected Folders And Include Their Subfolders

I'm writing a python script where I search a specific string across a tree directory. I ask the end-user to define which folders they would like to include in the search, and once

Solution 1:

I figured it out and it actually works well with just one for loop. Here is how the new code looks and hope it will help someone in the future...Best

startTime = datetime.datetime.now()
    option = input("Do you want to scan: A) Excel B) PDF C) Both Q) Quit: ")
    option = option.lower()
    if (option == "b") or (option == "b") or (option == "c"):
        stringToSearch = input("Which string do you want to search? ")
        folderToSearch = input("Which top folder to search from(i.e. Z:\S 15\BOMs)? ")
        subfoldersToSearch = input("Which subfolders(i.e. BOMs, Catalogs? <NO ANSWER = ALL) ")
        print("Press CTRL + C to stop the search")
        for foldername, subfolders, filenames in os.walk(folderToSearch, topdown=True):
            print(subfolders)
            for filename in filenames:
                if (subfoldersToSearch == "") or (subfoldersToSearch in foldername):
                    print(subfoldersToSearch, "+++", foldername)  

Post a Comment for "Python 3.5 Os.walk For Selected Folders And Include Their Subfolders"