Find Python files according to their content

时间:2019-04-23 15:19:35

标签: python

Using File Explorer in Windows, we can find files by typing part of the file names in the Search box. With the Advance option, we even can find a file according to its content.

Is it possible to search Python files based on their content without "manually opening each file and viewing it in a viewer or editor program"? I use Jupyter Lab to create Python files.

For example, I want to find python files that contain dayfirst.

Thanks for help.

2 个答案:

答案 0 :(得分:1)

看看pathlib

相关点/示例:

from pathlib import Path

p = Path('.')

files = [x for x in p.iterdir() if x.is_file()]
found_files = []

for file in files:
    with file.open() as f:
        for line in f:
            if 'dayfirst' in line:
                found_files.append(file)

答案 1 :(得分:0)

要使用Windows资源管理器启用内容搜索,可以设置Windows索引选项以包括.py文件的内容。这是逐步指南:

https://www.howtogeek.com/99406/how-to-search-for-text-inside-of-any-file-using-windows-search/

屏幕截图(用于批处理文件)...

Advanced Search Options

(还要确保保存.py文件的位置在Windows索引的位置。)