0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

指定ディレクトリ以下の__pycache__を再帰的に検索し削除したい

Posted at
search_and_destroy.py
import os, shutil
def search_and_destroy(search_list: list = ["__pycache__"], path: str = ".", verbose: bool = False):
    """指定されたファイルまたはディレクトリを検索して削除.

    Args:
        search_list (list, optional): 削除対象. Defaults to ["__pycache__"].
        path (str, optional): 開始パス. Defaults to ".".
        verbose (bool, optional): メッセージ. Defaults to False.
    """
    for root, dirs, files in os.walk(path):
        for search in search_list:
            # ディレクトリの削除
            if search in dirs:
                target_path = os.path.join(root, search)
                shutil.rmtree(target_path)
                if verbose:
                    print(f"destroyed directory: {target_path}")

            # ファイルの削除
            if search in files:
                target_path = os.path.join(root, search)
                os.remove(target_path)
                if verbose:
                    print(f"destroyed file: {target_path}")
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?