9
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

覚えたてのメソッドをアウトプット:relieved:

ディレクトリ内のファイル一覧取得

取得したファイル一覧がリストで返される。

リストは任意の順序で、ディレクトリ内に存在する特別なエントリは含まれません。
この関数の呼び出し中にファイルがディレクトリから削除されたり、ディレクトリに追加されたりした場合、そのファイルの名前が含まれるかどうかは未指定です。

import os

dir_path = "directory/"
files = os.listdir(dir_path)

print(files)
# ["main.py", "second.py", "third.py"]

ファイルの更新日時を取得

os.path.getmtime(path)

pathの最終変更時刻を返します。戻り値は、エポック (モジュールを参照) からの秒数を示す浮動小数点数です time。OSErrorファイルが存在しないかアクセスできない場合は例外を発生させます。

import os
import datetime

updated_time = os.path.getmtime("sample.csv")
# 1718536513.7453966
updated_time_dt = datetime.datetime.fromtimestamp(updated_time)
# 2024-06-16 20:15:13.745397

参考サイト

9
4
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
9
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?