23
16

More than 5 years have passed since last update.

Python覚書ファイルの更新日を取得する。

Posted at

os.stat

時間の取得について、ここでも書いていますが、もう少し書きます。
Python覚書:テキスト参照して、対象ファイルをコピーしつつファイル名を編集する

>>> os.stat(file_path).st_mtime
>>> 1442209206.0

変更された時刻が取得できる。でもこれは値のみ。

datetime.datetime.fromtimestamp

datetime.datetime.fromtimestampで変換する。

>>> datetime.datetime.fromtimestamp(os.stat(file_path).st_mtime)
datetime.datetime(2015, 9, 14, 14, 40, 6)

dt.strftime

さらにdt.strftimeで文字列に変換すれば出来上がり。

>>> dt.strftime('%Y/%m/%d  %H:%M:%S')
'2015/09/14  14:40:06'

パスから対象ファイルの日付を他のテキスト情報に書き出し処理。

gettime.py
# -*- coding: shift-jis -*-
def main():
    pass

if __name__ == '__main__':
    main()

import os
import datetime
from datetime import datetime as dt

os.chdir(r"D:\temp")

txt_file = open(r"D:\temp\photo.txt","r")

for target_line in txt_file:
    if target_line == '\n':
        break

    target = target_line[:-1]
    file_name = os.path.basename(target_line)[:-1]

    print file_name

    if os.path.exists(target) :
        dt = datetime.datetime.fromtimestamp(os.stat(target).st_mtime)
        key = dt.strftime('%Y/%m/%d  %H:%M:%S')
        txt_file_out = open(r"D:\python\py_date\ipad_img_file_date.csv", "a+")
        txt_file_out.write(target + "," + file_name + "," + str(dt) + "," + key + "\n")

txt_file_out.close()
txt_file.close()

エクセルでも似たような感じになる。

詳しくは後ほど。

import xlrd
year_b, month_b, day_b, hour_b, minute_b, second_b = xlrd.xldate_as_tuple(udtdate_ib,book.datemode)
as_udtdate_ib = str(year_b) + '/' + str(month_b) + '/' + str(day_b)
23
16
2

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
23
16