🦁
【Python】直近で更新があったファイルを表示する。
概要
・直近3分で更新など変更があったファイルを表示するプログラム
sample code
import os
import sys
import time
def check_recent_changes(folder_path, minutes=3):
"""
指定したフォルダ内で直近指定分数以内に変更があったファイルやフォルダを表示する。
:param folder_path: フォルダのパス
:param minutes: 直近の分数 (デフォルト: 3分)
"""
current_time = time.time()
threshold_time = current_time - (minutes * 60)
print(f"直近{minutes}分以内に変更があったファイルやフォルダ:")
for root, dirs, files in os.walk(folder_path):
for name in dirs + files:
path = os.path.join(root, name)
try:
# ファイルやフォルダの最終変更時刻を取得
last_modified_time = os.path.getmtime(path)
if last_modified_time >= threshold_time:
print(f"変更: {path}")
except Exception as e:
print(f"エラー: {path} - {e}")
if __name__ == "__main__":
# 引数でフォルダパスを指定
folder_path = sys.argv[1] if len(sys.argv) > 1 else "/tmp/"
check_recent_changes(folder_path)
補足
・引数指定なしの場合はデフォルで登録されているディレクトリを確認する。
(デフォルトでは/tmp/ )
Discussion