1
2

More than 3 years have passed since last update.

子のファイル最終更新日を親ディレクトリの更新日にセットする

Posted at

概要

多量にフォルダ分けされたファイルを管理する際に、フォルダ直下の最新ファイルの更新日が設定してあるとWindowsでソートする場合などで有用なので書いてみました。
1つ作っておけば、分類フォルダを見た時に途方に暮れることもなくなるかも?

ソース

import os
from pathlib import Path

#現在のパス直下を基準にディレクトリを探し、そのディレクトリ以下にあるファイル最新更新日を適用
p = Path("./")
for d in list(p.glob('*')):
    if d.is_dir :
        filedts = []
        for filep in list(d.glob('*')):
            filedts.append((filep.stat()).st_mtime)
        filedts.sort(reverse=True)
        if len(filedts) > 0 :
            print(d.name + ' ' + str(filedts[0]))
            os.utime(d,(filedts[0],filedts[0]))

Linux上にマウントしたNTFSで使用しています。
st_mtimeを使っていますがファイルシステムによって適宜atimeなりctimeなりに読み変えてください。

感想

Pathオブジェクトが何にでも使えて便利。どんどん使おう。

1
2
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
2