Python os.utime()方法

Python os.utime()方法

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

os.utime() 语法

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

os.utime() 参数

path:一个字符串或字节对象,表示有效的文件系统路径。
times(可选):一个2元组,形式为atime, mtime,每个成员是一个整数或float值,分别表示访问时间和修改时间,单位为秒。
ns(可选):一个2元组(atime_ns, mtime_ns),每个成员是一个整数或float值,分别表示访问时间和修改时间,单位为纳秒。
dir_fd:指向目录的文件描述符。默认值为“None”。
follow_symlinks:一个布尔值,True或False。If True方法将跟随符号链接,否则不跟随。

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

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

os.utime() 示例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

os.utime() 示例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/ library/os.html#os.utime

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程