0
1

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 1 year has passed since last update.

[Python] 備忘録 元のファイルの日付を出力するファイル名に入れる・・

Last updated at Posted at 2023-03-05

Pythonで出力するときに、元のファイルの日付・時間を使いたい場合がありますが
けっこう複雑ですぐに思い出せないので、備忘録として残すようにしました。

from datetime import date
import datetime
import os

###  filename='' 
filedate = datetime.datetime.fromtimestamp(os.path.getmtime(filename))

filedate.strftime("%Y-%m-%d %H:%M:%S")  # '2023-02-25 20:05:37'
filedate.strftime("%Y%m%d-%H%M")  # '20230225-2005'

2023/6/10 追記
ついでに、本日の日付をフォルダやファイル名にするのは

from datetime import date
import datetime

today=datetime.datetime.now()

today.strftime("%Y%m%d-%H%M")  # '20230610-1435'

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?