Python Glossary Русский

all Python 3osfunction

os.utime

os — metadata

Set a file's access and modification times (atime, mtime).

Syntax

os.utime(...)

Without a second argument it sets the current time — what touch does to an existing file. The (atime, mtime) tuple takes seconds and the ns parameter takes nanoseconds; you cannot pass both. Build systems and rsync compare mtime, so utime helps preserve original dates when copying — which is what shutil.copystat does.

Examples

import os
open('stamp.txt', 'w').close()
os.utime('stamp.txt', (100, 200))
print(os.path.getmtime('stamp.txt'))   # → 200.0
os.remove('stamp.txt')

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy