Python で os.path.splitext を使ってみる
Python
Published: 2019-09-09

やったこと

os.path.splitext を使ってみます。

確認環境

$ python
Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 21 2017, 18:29:43)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

調査

>>> import os
>>> os.path.splitext('hogehoge/hoge2/sample.jpg')
('hogehoge/hoge2/sample', '.jpg')
>>> os.path.splitext('hogehoge/hoge2/sample.jpg')[0]
'hogehoge/hoge2/sample'
>>> os.path.splitext('hogehoge/hoge2/sample.jpg')[1]
'.jpg'
>>> os.path.splitext('~/.bashrc')
('~/.bashrc', '')

ファイルのパスと拡張子を分割します。

参考