import os
from datetime import datetime
now = datetime.now()
for dpath,dnames,fnames in os.walk('./', topdown=False) :
for fname in fnames:
filepath = os.path.join(dpath, fname)
stat = os.stat(filepath)
lastAccess = datetime.fromtimestamp(stat.st_mtime)
age = now - lastAccess
print(filepath + '--- last update ' + str(age.days) + ' days ago.')
for dname in dnames :
dirpath = os.path.join(dpath, dname)
files = len(os.listdir(dirpath))
print(dirpath + '--- has ' + str(files) + ' files.')
./.settings/org.eclipse.core.resources.prefs--- last update 4 days ago.
./__pycache__/myDateConv.cpython-32.pyc--- last update 4 days ago.
./__pycache__/myDateConvTest.cpython-32.pyc--- last update 4 days ago.
./.project--- last update 34 days ago.
./.pydevproject--- last update 34 days ago.
./mailexample.py--- last update 4 days ago.
./myDateConv.py--- last update 4 days ago.
./myDateConvTest.py--- last update 4 days ago.
./walk.py--- last update 0 days ago.
./.settings--- has 1 files.
./__pycache__--- has 2 files.
- os.work() に topdown=False を指定すると、末端から列挙される。(再帰的にファイルとディレクトリを消していくときなどに便利)
- POSIX Time から datetime に変換するには datetime = datetime.fromtimestamp(sec)
Python