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?

Pythonで不要ファイルをまとめて削除するコード

0
Last updated at Posted at 2025-12-07

指定パス以下のフォルダ内のファイルを深さ無制限で含め、パターンにマッチする全ファイルをまとめて削除するPythonコード
実行すると一覧表示され、y/N 選択で y を入力すると、すべて削除されます。

注意) 削除したファイルは復元できません。

#delete_file.py
import os
import fnmatch

# 適宜変更してください(このパス以下のフォルダ内のファイルも削除対象になります)
folder = r"C:\path\to\folder"
# ---------------------------------------------------------------------

# —————————–
# 設定
# —————————–
delete_patterns = [
   "*.tmp",
   "log_*.txt",
   "backup_*.zip"
]

dry_run = True  # True: 一覧表示後に確認, False: 即削除

def is_delete_file(filename):
    """削除対象かどうか判定(ワイルドカード)"""
    return any(fnmatch.fnmatch(filename, pattern) for pattern in delete_patterns)

# -----------------------------
# 削除候補収集(深さ無制限)
# -----------------------------
delete_candidates = []

for root, dirs, files in os.walk(folder):
    for filename in files:
        if is_delete_file(filename):
            path = os.path.join(root, filename)
            delete_candidates.append(path)

# -----------------------------
# dry_run=True → 一覧表示 + 確認プロンプト
# -----------------------------
if dry_run:
    if delete_candidates:
        print("削除候補一覧:")
        for path in delete_candidates:
            print(f"- {path}")
        print(f"\n合計 {len(delete_candidates)} 件")

        # 確認プロンプト
        ans = input("\n上記のファイルを削除しますか? [y/N]: ").strip().lower()
        if ans == "y":
            for path in delete_candidates:
                os.remove(path)
            print(f"{len(delete_candidates)} 件のファイルを削除しました。")
        else:
            print("削除をキャンセルしました。")
    else:
        print("削除対象はありません。")

# -----------------------------
# dry_run=False → 即削除
# -----------------------------
else:
    for path in delete_candidates:
        os.remove(path)
    print(f"{len(delete_candidates)} 件のファイルを削除しました。")
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?