ファイルパスから多種の情報を取得する os.path

しょっちゅう使いそうなのでメモ
os をインポート

import os

ファイルパスから、ディレクトリパス

dirpath = os.path.os.path.dirname(filepath)

ファイルパスから、ファイル名

filename = os.path.basename(filepath)

ファイルパスから、拡張子とそれ以外に分割したタプル→拡張子

filetbl = os.path.splitext(filepath)

filetbl[0] = それ以外
filetbl[0] = 拡張子( ".log" など、先頭文字が「.」がつく)

拡張子の「.」無しを求める

filesuffix = os.path.splitext(filepath)[1][1:]

拡張子を変えたファイルパスを生成する

textpath = os.path.splitext(filepath)[0] + ".txt"

ファイルサイズ(単位:byte)

filesize = os.path.getsize(filepath)

ファイル更新時刻

import os
import datetime
mtime = os.path.getmtime(self.file)
# type(mtime) -> float
mtimestr = datetime.datetime.fromtimestamp(mtime)

同じフォルダの別のファイルのパス文字列を作成
os.path.join を使う

otherpath = os.path.join(os.path.dirname(filepath), 'otherfile')