Python3-os-utime

提供:Dev Guides
移動先:案内検索

Python 3-os.utime()メソッド

説明

メソッド* utime()*は、pathで指定されたファイルのアクセス時間と変更時間を設定します。

構文

以下は* utime()*メソッドの構文です-

os.utime(path, times)

パラメーター

  • path -これはファイルのパスです。
  • times -これはファイルアクセスと変更時間です。 時間がゼロの場合、ファイルアクセス時間と変更時間は現在の時間に設定されます。 パラメーターtimeは、(atime、mtime)の形式の行で構成されます(つまり、accesstime、modifiedtime)。

戻り値

このメソッドは値を返しません。

次の例は、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!!