在函数

时间:2018-05-29 21:50:01

标签: python python-3.x function debugging

由于我无法解决的原因,我的第一个for循环中的“subdirs”变量在写入函数内部时变为“未使用的变量”,这导致我的目录搜索不完整以实现目标功能。但是当它不是函数的一部分时,它被识别为变量,我的代码能够成功搜索整个目录并执行所需的任务。我是python的新手,请让我知道如何修复我的功能,因此“subdirs”将被识别为变量。非常感谢!

我在For of Loop中使用“Subdirs”在函数

import os

def function(rootdir, keyPhrases):

    path = rootdir # Enter the root directory you want to search from

    key_phrases = [keyPhrases] # Enter here the key phrases in the lines you hope to find 

    # This for loop allows all sub directories and files to be searched
    for (path, subdirs, files) in os.walk(path): 
        files = [f for f in os.listdir(path) if f.endswith('.txt') or f.endswith('.log')] # Specify here the format of files you hope to search from (ex: ".txt" or ".log")
        files.sort() # file is sorted list

        files = [os.path.join(path, name) for name in files] # Joins the path and the name, so the files can be opened and scanned by the open() function

        # The following for loop searches all files with the selected format
        for filename in files:

                # Opens the individual files and to read their lines
                with open(filename) as f:
                    f = f.readlines()

                # The following loop scans for the key phrases entered by the user in every line of the files searched, and stores the lines that match into the "important" array
                for line in f:
                    for phrase in key_phrases: 
                        if phrase in line:
                            print(line)
                            break 

    print("The end of the directory has been reached, if no lines are printed then that means the key phrase does not exist in the root directory you entered.")

My For循环与“Subdirs”本身

import os

path = r"D:\(Chosen Root Directory)"

key_phrases = ["example_keyPhrase1"] # Enter here the key phrases in the lines you hope to find 

    # This for loop allows all sub directories and files to be searched
for (path, subdirs, files) in os.walk(path): 
    files = [f for f in os.listdir(path) if f.endswith('.txt') or f.endswith('.log')] # Specify here the format of files you hope to search from (ex: ".txt" or ".log")
    files.sort() # file is sorted list

    files = [os.path.join(path, name) for name in files] # Joins the path and the name, so the files can be opened and scanned by the open() function

    # The following for loop searches all files with the selected format
    for filename in files:

        # Opens the individual files and to read their lines
        with open(filename) as f:
            f = f.readlines()

            # The following loop scans for the key phrases entered by the user in every line of the files searched, and stores the lines that match into the "important" array
            for line in f:
                for phrase in key_phrases: 
                    if phrase in line:
                        print(line)
                        break 

print("The end of the directory has been reached, if no lines are printed then that means the key phrase does not exist in the root directory you entered.")

1 个答案:

答案 0 :(得分:1)

您永远不会在第一个代码中引用子目录,这就是您收到警告的原因。但是,这并不是您的代码无法按预期工作的原因。

这一行是问题所在:files = [f for f in os.listdir(path) if f.endswith('.txt') or f.endswith('.log')]

os.listdir(path)将列出该路径[1]中的所有目录,不包括每个目录中的子目录。变量'subdirs'已包含该信息。但你不需要它。变量'files'已经有你想要的,所以你可以改变这一行:

files = [f for f in files if f.endswith('.txt') or f.endswith('.log')]

[1] https://docs.python.org/3/library/os.html#os.listdir