首页 > 解决方案 > 写入函数时出现警告“未使用的变量 'subdirs'”

问题描述

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

我的函数中带有“子目录”的 For 循环

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

我的带有“子目录”的 For 循环本身

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

标签: pythonpython-3.xfunctiondebugging

解决方案


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

这一行是问题: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


推荐阅读