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
相關用法
- Python set()用法及代碼示例
- Python next()用法及代碼示例
- Python os.dup()用法及代碼示例
- Python getattr()用法及代碼示例
- Python os.getpgrp()用法及代碼示例
- Python os.fork()用法及代碼示例
- Python os.nice()用法及代碼示例
- Python os.getsid()用法及代碼示例
- Python os.setregid()用法及代碼示例
- Python os.pwrite()用法及代碼示例
- Python os.writev()用法及代碼示例
- Python os.readv()用法及代碼示例
- Python sympy.det()用法及代碼示例
- Python PIL ImageOps.fit()用法及代碼示例
注:本文由純淨天空篩選整理自ihritik大神的英文原創作品 Python | os.utime() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。