Python 3.5 OS.Walk for selected folders and include their subfolders

大城市里の小女人 提交于 2020-03-26 07:54:08

问题


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 the script finds one of the folders that the user would like to scan, the script is supposed to also scan all the sub-folders for this selected folder.

I'm trying to do a couple of for loops, but I can't get it to work.

The beginning of the script looks like:

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):
        for filename in filenames:
            if (subfoldersToSearch == "") or (subfoldersToSearch in foldername):
                print(subfoldersToSearch, "+++", foldername)                      
                for x_foldername, x_subfolders, x_filenames in os.walk(foldername):
                    totalFiles += 1
                    for x_filename in x_filenames:
                        if (x_filename.endswith('.pdf') and option == "b") or (x_filename.endswith('.pdf') and option == "c"):

[Do remaining stuff]

The problem is that it gets into a continuous loop because as soon as it's done walking through one of the selected folders, it comes back to the first for loop and it tries to walk the same selected folder again.

Is there a better way to do this os.walk?

Basically, I would like the script to find a specific folder and then scan the contents of that folder (including folders), and then to keep going to the next folder without starting all over again.


回答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)  


来源:https://stackoverflow.com/questions/40229365/python-3-5-os-walk-for-selected-folders-and-include-their-subfolders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!