简体   繁体   English

Python 3.5 OS.Walk用于选定的文件夹并包括其子文件夹

[英]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. 我正在编写一个python脚本,在其中搜索树目录中的特定字符串。 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. 我正在尝试做一些for循环,但是我无法使其正常工作。

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. 问题在于它进入了一个连续循环,因为一旦遍历一个选定的文件夹,它就会回到第一个for循环,并尝试再次遍历相同的选定文件夹。

Is there a better way to do this os.walk ? 有没有更好的方法来执行此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. 基本上,我希望脚本找到一个特定的文件夹,然后扫描该文件夹(包括文件夹)的内容,然后继续转到下一个文件夹,而无需重新开始。

I figured it out and it actually works well with just one for loop. 我想通了,它实际上仅与一个for循环一起使用就很好。 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)  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM