Python Glossary Русский

all Python 3osfunction

os.path.getmtime

os.path — metadata (stat)

Return the last-modification time of a file (seconds since the epoch).

Syntax

os.path.getmtime(path)

Precision depends on the filesystem: FAT has two-second resolution, so equality of times is an unreliable "file changed" test, and build tools compare the size as well. The value is seconds since the UTC epoch; pass it to datetime.fromtimestamp for a readable form. For nanosecond precision use os.stat(path).st_mtime_ns.

Examples

import os
import time
print(os.path.getmtime(os.__file__) > 0)   # → True
print(os.path.getmtime(os.__file__) == os.stat(os.__file__).st_mtime)   # → True: getmtime — это os.stat(path).st_mtime
print(os.path.getmtime(os.__file__) <= time.time())   # → True: файл изменён не позже текущего момента
print(time.strftime('%Y-%m-%d', time.localtime(os.path.getmtime(os.__file__))))   # → дата последнего изменения, например 2026-05-14
print(os.path.getmtime('нет-такого-файла'))   # → FileNotFoundError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy