8
11

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 5 years have passed since last update.

No.043【Python】パス文字列からファイル名/フォルダ名/拡張子取得

Posted at

python-logo-master-v3-TM-flattened.png

####今回は、ファイル名、フォルダ名、拡張子の取得について書いていきます。
I'll write about how to acquire file, folder, and extension names

###■ osによるパスの区切り文字の違い
 Difference of Path-delimiter by os

>>> import os
>>> 
>>> filepath = "./dir/subdir/filename.ext"
>>> 
>>> 
>>> # osによるパスの区切り文字の違い
>>> 
>>> print(os.sep)
/
>>> 
>>> print(os.sep is os.path.sep)
True
>>> # ↑区切り文字は os.sep または os.path.sepで取得、確認できる

###■ ファイル名の取得: os.path.basename( )
 Acquire a file name

>>> # os.path.basename( ):パス文字列からファイル名取得の場合
>>> 
>>> basename = os.path.basename(filepath)
>>> 
>>> print(basename)
filename.ext
>>> 
>>> print(type(basename))
<class 'str'>

###■ フォルダ名の取得: os.path.dirname()
 Acquire a folder name

>>> # os.path.dirname:パス文字列からフォルダ名取得の場合
>>> 
>>> dirname = os.path.dirname(filepath)
>>> 
>>> print(dirname)
./dir/subdir
>>> 
>>> print(type(dirname))
<class 'str'>
>>> 
>>> # os.path.basename():ファイル直上のフォルダ名のみ取得の場合
>>> 
>>> subdirname = os.path.basename(os.path.dirname(filepath))
>>> 
>>> print(subdirname)
subdir

###■ ファイル・フォルダ名の同時取得: os.path.split()
 Acquire file and folder's names

>>> # os.path.split():ファイル・フォルダ名両方取得の場合
>>> 
>>> base_dir_pair = os.path.split(filepath)
>>> 
>>> print(base_dir_pair)
('./dir/subdir', 'filename.ext')
>>> 
>>> print(type(base_dir_pair))
<class 'tuple'>
>>> 
>>> print(os.path.split(filepath)[0] == os.path.dirname(filepath))
True
>>> 
>>> print(os.path.split(filepath)[1] == os.path.basename(filepath))
True
>>> 
>>> # タプルのアンパック利用による変数代入も可能
>>> 
>>> dirname, basename = os.path.split(filepath)
>>> 
>>> print(dirname)
./dir/subdir
>>> 
>>> print(basename)
filename.ext

###■ パス文字列がフォルダを示す場合
 The case that path strings indicate folders

>>> dirpath_without_sep = '../dir/subdir'
>>> 
>>> print(os.path.split(dirpath_without_sep))
('../dir', 'subdir')
>>> 
>>> print(os.path.basename(dirpath_without_sep))
subdir
>>> 
>>> 
>>> # os.path.dirname():最下層のフォルダ名を取得の場合
>>> 
>>> dirpath_with_sep = '../dir/subdir/'
>>> 
>>> print(os.path.split(dirpath_with_sep))
('../dir/subdir', '')
>>> 
>>> print(os.path.basename(os.path.dirname(dirpath_with_sep)))
subdir

###■ 拡張子を取得: os.path.splitext()
 Acquire extension

>>> # os.path.splitext():拡張子の取得の場合
>>> 
>>> root_ext_pair = os.path.splitext(filepath)
>>> 
>>> print(root_ext_pair)
('./dir/subdir/filename', '.ext')
>>> 
>>> print(type(root_ext_pair))
<class 'tuple'>
>>> 
>>> # +演算子による結合:元のパス文字へ戻る
>>> 
>>> root, ext = os.path.splitext(filepath)
>>> 
>>> print(path)
./dir/subdir/filename.ext

###■ 拡張子を変更したパス文字列作成
 How to make path-strings,which changes extension

>>> # 元のパス文字列から拡張子だけを変更したパス文字列を作成する場合
>>> # os.path.splitext()によるタプルの要素と拡張子を結合する
>>> 
>>> other_ext_filepath = os.path.splitext(filepath)[0] + '.jpg'
>>> 
>>> print(other_ext_filepath)
./dir/subdir/filename.jpg

###■ ピリオドなしの拡張子取得
 Acquire extension without period

>>> # ピリオドなしの拡張子を取得の場合、[1:]で2文字目以降を指定
>>> 
>>> ext_without_period = os.path.splitext(filepath)[1][1:]
>>> 
>>> print(ext_without_period)
ext

###■ ファイル・フォルダ名を結合によるパス文字列を作成: os.path.join()
 How to make path-strings with the combiation with file or folder names

>>> # os.path.join()
>>> # os.path.join():ファイル名とフォルダ名を結合し、新たなパス文字列を作成の場合
>>> 
>>> path = os.path.join('dir', 'subdir', 'filename.ext')
>>> 
>>> print(path)
dir/subdir/filename.ext

###■ 同じフォルダの別ファイルのパス文字列作成
 How to make another file's path-strings within the same folder

>>> # os.path.dirname()とos.path.join()の組み合わせ
>>> # 特定のファイルと同一フォルダの別のファイルのパス文字列を作成の場合
>>> 
>>> other_filepath = os.path.join(os.path.dirname(filepath), 'other_file.ext')
>>> 
>>> print(other_filepath)
./dir/subdir/other_file.ext

随時に更新していきますので、
定期的な購読をよろしくお願いします。
I'll update my article at all times.
So, please subscribe my articles from now on.

本記事について、
何か要望等ありましたら、気軽にメッセージをください!
If you have some requests, please leave some messages! by You-Tarin

8
11
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
8
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?