El módulo OS en Python proporciona funciones para interactuar con el sistema operativo. OS viene bajo los módulos de utilidad estándar de Python. Este módulo proporciona una forma portátil de usar la funcionalidad dependiente del sistema operativo.
os.utime()
El método del módulo os en Python se usa para establecer el acceso y el tiempo de modificación de la ruta especificada.
Sintaxis: os.utime(ruta, veces = Ninguno, *, [ns, ]dir_fd = Ninguno, seguir_enlaces simbólicos = Verdadero)
Parámetro:
ruta : una string o un objeto de bytes que representa una ruta válida del sistema de archivos.
times (opcional): una tupla de 2 de la forma (atime, mtime) donde cada miembro es un valor entero o flotante que representa el tiempo de acceso y el tiempo de modificación en segundos, respectivamente.
ns (opcional): una tupla de 2 de la forma (atime_ns, mtime_ns) donde cada miembro es un valor entero o flotante que representa el tiempo de acceso y el tiempo de modificación en nanosegundos, respectivamente.
dir_fd: un descriptor de archivo que hace referencia a un directorio. El valor predeterminado de este parámetro es Ninguno.
follow_symlinks: un valor booleano, ya sea verdadero o falso. Si el método True seguirá el enlace simbólico, de lo contrario no.Tipo de devolución: este método no devuelve ningún valor
Código #1: Uso del os.utime()
método
# Python program to explain os.utime() method # importing os module import os # Path path = '/home / ihritik / Documents / file.txt' # Print current access and modification time # of the above specified path print("Current access time:", os.stat(path).st_atime) print("Current modification time:", os.stat(path).st_mtime) # Access time in seconds atime = 200000000 # Modification time in seconds mtime = 100000000 # Set the access time and # modification time for the # above specified path # using os.utime() method tup = (atime, mtime) os.utime(path, tup) print("\nAccess and modification time changed\n") # Print current access and modification time print("Current access time:", os.stat(path).st_atime) print("Current modification time:", os.stat(path).st_mtime) # Either we can specify times # or specify ns parameter. # It is an error to specify # tuples for both times and ns
Current access time (in seconds): 1568930018.710342 Current modification time (in seconds): 1568930018.610892 Access and modification time changed Current access time (in seconds): 200000000.0 Current modification time (in seconds): 100000000.0
Código #2: si se especifica el parámetro ns,
# Python program to explain os.utime() method # importing os module import os # Path path = '/home / ihritik / Documents / file.txt' # Print current access and modification time # of the above specified path print("Current access time (in seconds):", os.stat(path).st_atime) print("Current modification time (in seconds):", os.stat(path).st_mtime) # Access time in nanoseconds atime_ns = 20000000012345 # Modification time in nanoseconds mtime_ns = 10000000012345 # Set the access time and # modification time in nanoseconds # for the above specified path # using os.utime() method # (ns is keyword only argument) tup = (atime_ns, mtime_ns) os.utime(path, ns = tup) print("\nAccess and modification time changed\n") # Print current access and modification time print("Current access time (in seconds):", os.stat(path).st_atime) print("Current modification time (in seconds):", os.stat(path).st_mtime) # Either we can specify times # or specify ns parameter. # It is an error to specify # tuples for both times and ns
Current access time (in seconds): 1568930018.710342 Current modification time (in seconds): 1568930018.610892 Access and modification time changed Current access time (in seconds): 20000.000012345 Current modification time (in seconds): 10000.000012345
Código n.º 3: si el parámetro times es Ninguno y el parámetro ns no está especificado
# Python program to explain os.utime() method # importing os module import os # Path path = '/home / ihritik / Documents / file.txt' # Print current access and modification time # of the above specified path print("Current access time (in seconds):", os.stat(path).st_atime) print("Current modification time (in seconds):", os.stat(path).st_mtime) # Set the access time and # modification time in nanoseconds # for the above specified path # using os.utime() method os.utime(path) print("\nAccess and modification time changed\n") # Print current access and modification time print("Current access time (in seconds):", os.stat(path).st_atime) print("Current modification time (in seconds):", os.stat(path).st_mtime) # If times is None and ns is unspecified, # then it will be equivalent to # specifying ns = (atime_ns, mtime_ns) # where member atime_ns and mtime_ns # are current time in nanoseconds
Current access time (in seconds): 20000.000012345 Current modification time (in seconds): 10000.000012345 Access and modification time changed Current access time (in seconds): 1568930018.710342 Current modification time (in seconds): 1568930018.610892
Referencia: https://docs.python.org/3/library/os.html#os.utime