当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python os.utime()用法及代码示例


Python中的OS模块提供了与操作系统进行交互的函数。操作系统属于Python的标准实用程序模块。该模块提供了使用依赖于操作系统的函数的便携式方法。

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

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

参数:
path:表示有效文件系统路径的字符串或字节对象。
times(可选):格式为(atime,mtime)的2元组,其中每个成员都是整数或浮点值,分别表示访问时间和修改时间(以秒为单位)。
ns(可选):格式为(atime_ns,mtime_ns)的2元组,其中每个成员都是整数或浮点值,分别表示访问时间和修改时间(以纳秒为单位)。
dir_fd:引用目录的文件描述符。此参数的默认值为“无”。
follow_symlinks:布尔值True或False。如果为True,则方法将遵循符号链接,否则不进行。

返回类型:此方法不返回任何值

代码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



相关用法


注:本文由纯净天空筛选整理自ihritik大神的英文原创作品 Python | os.utime() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。