當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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


描述

方法utime()設置路徑指定的文件的訪問和修改時間。

用法

以下是語法utime()方法 -

os.utime(path, times)

參數

  • path- 這是文件的路徑。

  • times- 這是文件訪問和修改時間。如果時間為無,則文件訪問和修改時間設置為當前時間。參數時間由(atime,mtime)形式的行組成,即(訪問時間,修改時間)。

返回值

此方法不返回任何值。

示例

下麵的例子展示了 utime() 方法的用法。

# !/usr/bin/python3
import os, sys, time
os.chdir("d:\\tmp")

# Showing stat information of file
stinfo = os.stat('foo.txt')
print (stinfo)

# Using os.stat to recieve atime and mtime of file
print ("access time of foo.txt:%s" %stinfo.st_atime)
print ("modified time of foo.txt:%s" %stinfo.st_mtime)
print (time.asctime( time.localtime(stinfo.st_atime)))

# Modifying atime and mtime
os.utime("foo.txt",(1330712280, 1330712292))
print ("after modification")
print (time.asctime( time.localtime(stinfo.st_atime)))
print ("done!!")

結果

當我們運行上述程序時,它會產生以下結果 -

os.stat_result(st_mode = 33206, st_ino = 1688849860351098, 
   st_dev = 1017554828, st_nlink = 1, st_uid = 0, st_gid = 0, st_size = 0,
   st_atime = 1455684273, st_mtime = 1455684273, st_ctime = 1455683589)
access time of foo.txt:1455684273.84375
modified time of foo.txt:1455684273.84375
Wed Feb 17 10:14:33 2016
after modification
Fri Mar  2 23:48:00 2012
done!!

相關用法


注:本文由純淨天空篩選整理自 Python 3 - os.utime() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。