Getting last modified for every file in a directory
You may also use lstat().st_mtime
for a WindowsPath
(pathlib.Path) object.
Example:
from pathlib import Path
file = Path(r'C:\Users\<user>\Desktop\file.txt')
file.lstat().st_mtime
Output: 1496134873.8279443
import datetime
datetime.datetime.fromtimestamp(file.lstat().st_mtime)
Output: datetime.datetime(2017, 5, 30, 12, 1, 13, 827944)
There is no need to use os.stat
function, pathlib has the same function- file.stat()
where file is you path object.
You can use the following code:
for file in asm_pths:
(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = file.stat()
print(f'last modified: {time.ctime(mtime)}')
But if you only want last modification date then use this:
for file in asm_pths:
print(f'last modified: {time.ctime(file.stat().st_mtime)}')
I would prefer to avoid using os.path and pathlib.Path in the same project as much as possible in order to prevent confusion and bugs, because pathlib's paths are made of Path objects and os.path is expecting strings as paths.