在目录的文件中搜索字符串

时间:2018-01-05 09:38:28

标签: python python-3.x

我需要帮助编写轻量级Python(v3.6.4)脚本来搜索文件和文件夹目录中的单个关键字。目前,我使用Notepad ++搜索文件目录,虽然我相信Python脚本会更快?

当前脚本:

import os
key = input("Search For?: ")
folder = os.listdir("/")
for line in folder:
    if key in line:
        print(line)

编辑:我正在使用Notepad ++来运行这些搜索查询。

我想要搜索的目录在文件夹中有多个级别的文件。

1 个答案:

答案 0 :(得分:2)

你应该真的使用grep(即grep -Ril "keyword" /),或者,如果在Windows上,findstrfindstr /I /M /C:"keyword" /S \*),但如果你坚持用Python做,你会想要递归地通过根目录使用os.walk() walk ,然后打开每个找到的文件并迭代它以查找它是否包含您想要的关键字,如:

import os

keyword = input("Search For?: ")  # ask the user for keyword, use raw_input() on Python 2.x

root_dir = "/"  # path to the root directory to search
for root, dirs, files in os.walk(root_dir, onerror=None):  # walk the root dir
    for filename in files:  # iterate over the files in the current dir
        file_path = os.path.join(root, filename)  # build the file path
        try:
            with open(file_path, "rb") as f:  # open the file for reading
                # read the file line by line
                for line in f:  # use: for i, line in enumerate(f) if you need line numbers
                    try:
                        line = line.decode("utf-8")  # try to decode the contents to utf-8
                    except ValueError:  # decoding failed, skip the line
                        continue
                    if keyword in line:  # if the keyword exists on the current line...
                        print(file_path)  # print the file path
                        break  # no need to iterate over the rest of the file
        except (IOError, OSError):  # ignore read and permission errors
            pass

TEST :我刚刚测试了它在我的本地系统上搜索PyEnum_TypeCPython source code克隆到F:\.tmp\cpython-master(因此root_dir = r"F:\.tmp\cpython-master")运行CPython 3.5.1,结果如预期:

Search For?: PyEnum_Type
F:\.tmp\cpython-master\Include\enumobject.h
F:\.tmp\cpython-master\Objects\enumobject.c
F:\.tmp\cpython-master\Objects\object.c
F:\.tmp\cpython-master\PC\python3.def
F:\.tmp\cpython-master\Python\bltinmodule.c

如果它没有产生任何结果,您最有可能设置错误的路径,或者您正在搜索定义的root_dir下的文件中不存在的内容(或者您的用户没有可以访问它们。)