Python 3.5 OS.Walk用于选定的文件夹并包括其子文件夹 [英] Python 3.5 OS.Walk for selected folders and include their subfolders

查看:135
本文介绍了Python 3.5 OS.Walk用于选定的文件夹并包括其子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写python脚本,在其中搜索树目录中的特定字符串。我要求最终用户定义他们想在搜索中包括哪些文件夹,一旦脚本找到了用户想要扫描的文件夹之一,脚本就应该为此扫描所有子文件夹。

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.

我试图做一些for循环,但是我无法使其正常工作。

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

脚本的开头类似于:

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"):

[做剩余的事情]

问题在于它进入了一个连续循环,因为一旦完成选择的一个文件夹,它就会出现回到第一个for循环,它会尝试再次遍历相同的选定文件夹。

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.

是否有更好的方法来执行此 os.walk

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.

推荐答案

I弄清楚了,它实际上只适用于一个for循环。这是新代码的外观,希望能对将来的人有所帮助...最佳

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)  

这篇关于Python 3.5 OS.Walk用于选定的文件夹并包括其子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆