os.utime()

os.utime(path, times=None, *, [ns, ]dir_fd=None, follow_symlinks=True)

Set the access and modified times of the file specified by path.

utime() takes two optional parameters, times and ns. These specify the times set on path and are used as follows:

  • If ns is specified, it must be a 2-tuple of the form (atime_ns, mtime_ns) where each member is an int expressing nanoseconds.
  • If times is not None, it must be a 2-tuple of the form (atime, mtime) where each member is an int or float expressing seconds.
  • If times is None and ns is unspecified, this is equivalent to specifying ns=(atime_ns, mtime_ns) where both times are the current time.

It is an error to specify tuples for both times and ns.

Whether a directory can be given for path depends on whether the operating system implements directories as files (for example, Windows does not). Note that the exact times you set here may not be returned by a subsequent stat() call, depending on the resolution with which your operating system records access and modification times; see stat(). The best way to preserve exact times is to use the st_atime_ns and st_mtime_ns fields from the os.stat() result object with the ns parameter to utime.

This function can support specifying a file descriptor, paths relative to directory descriptors and not following symlinks.

New in version 3.3: Added support for specifying an open file descriptor for path, and the dir_fd, follow_symlinks, and ns parameters.

doc_python
2016-10-07 17:40:10
Comments
Leave a Comment

Please login to continue.