5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ファイルの日付取得

Last updated at Posted at 2019-09-16

ファイルの作成日、更新日、アクセス日の取得

ファイルの日付の取得方法
Windowsの場合です。

test.py
from pathlib import Path
import datetime

p = Path(r'c:\test\test.txt')

# 作成日
create_time = datetime.datetime.fromtimestamp(p.stat().st_ctime)
# 更新日
update_time = datetime.datetime.fromtimestamp(p.stat().st_mtime)
# アクセス日
access_time = datetime.datetime.fromtimestamp(p.stat().st_atime)

print('作成日', create_time)
print('更新日', update_time)
print('アクセス日', access_time)

日付取得

pathlibを使用する。
Path.stat()でパスに関する情報を取得。ここに日付に関する情報がある。
statのプロパティで以下が日付に関する情報となる。

  • st_ctime:作成日
  • st_mtime:更新日
  • st_atime:アクセス日

windowsの場合は、st_ctimeは作成日となる。osによって内容が変わる。

エポック秒の変換

取得した時間はエポック秒となっているため、変換を行う。
datetimeを使用
datetime.datetime.fromtimestamp()により変換する。

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?