📜  Python| os.utime() 方法

📅  最后修改于: 2022-05-13 01:54:30.061000             🧑  作者: Mango

Python| os.utime() 方法

Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。

Python中os模块os.utime()方法用于设置指定路径的访问和修改时间。

代码 #1:使用os.utime()方法

# 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

代码#2:如果指定了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

代码 #3:如果 times 参数为 None 且 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)
  
  
# 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

参考: https://docs。 Python.org/3/library/os.html#os.utime