Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Kiểm tra thời gian truy cập file cuối cùng trong Python (os.path.getmtime)

Dùng os.path.getmtime(path) trả về timestamp (float, seconds since epoch) của lần sửa đổi cuối (modification time).

Ví dụ

import os
import time

mtime = os.path.getmtime('my_file.txt')
print(mtime)                    # 1505928275.3081832
print(time.ctime(mtime))        # Mon Jan 20 10:30:45 2025
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(mtime)))

Các time liên quan

  • os.path.getmtime(path) — modification time (mtime).
  • os.path.getatime(path) — access time (atime).
  • os.path.getctime(path) — creation time (Windows) hoặc metadata change time (Unix).

Dùng os.stat() lấy tất cả

st = os.stat('my_file.txt')
print(st.st_mtime, st.st_atime, st.st_ctime, st.st_size)