LoginSignup
1
0

More than 1 year has passed since last update.

Pythonで特定ディレクトリ内の特定文字列を含むファイルを列挙する

Last updated at Posted at 2022-01-31

表題の通り,pythonで特定ディレクトリ内の任意の深さにあるファイルを列挙するプログラムを書きました.

実行例は以下の通りです.

# pathはホームディレクトリを起点として考えています.
$ python3 find.py --word "word to search!!!" --path target_directory

コードは以下の通りです.

find.py
import os
from argparse import ArgumentParser
from typing import Tuple


def _is_word_in_file(word: str, file: str) -> bool:
    with open(file, 'r') as f:
        for line in f:
            if word in line:
                return True
        else:
            return False


def _get_args() -> Tuple[str, str, str]:
    parser = ArgumentParser()
    parser.add_argument(
        '--path',
        required=True,
        type=str,
        help='The path of interest'
    )
    parser.add_argument(
        '--word',
        required=True,
        type=str,
        help='The word to check whether each file has'
    )
    parser.add_argument(
        '--ext',
        default='.py',
        type=str,
        help='The extension of files to check such as `.tex`, `.py`, `.csv`.\n'
             'You can omit the period and just parse such as `tex`, `py`, `csv` as well'
    )
    try:
        args = parser.parse_args()
    except:
        import sys
        parser.print_help()
        sys.exit()

    path, word, ext = args.path, args.word, args.ext
    if not ext.startswith('.'):
        ext = '.' + ext

    return path, word, ext


if __name__ == '__main__':
    path, word, ext = _get_args()
    path = path if 'home' in path else os.path.join(os.environ['HOME'], path)

    for root, dirs, files in os.walk(path): 
        if len(files) == 0:
            continue

        target_files = [os.path.join(root, fn) for fn in files if fn.endswith(ext)]
        passed_files = [fn for fn in target_files if _is_word_in_file(word, fn)]

        if len(passed_files) > 0:
            print('\n'.join(passed_files))
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0