python3

python3 で、ファイルのパスから、拡張子を取り除く方法

投稿日:

os モジュールを使って、ファイル操作をすることができる。

ファイル名にドット. が複数の拡張子がついている場合には、拡張子を取り除く操作を何回か繰り返す。

例

import os
path0 = "/path/to/some/my_file.tar.gz.zip"
print('path0: ' + path0)
path1 = os.path.splitext(path0)[0]
print('path1: ' + path1)
path2 = os.path.splitext(path1)[0]
print('path2: ' + path2)
path3 = os.path.splitext(path2)[0]
print('path3: ' + path3)

結果

path0: /path/to/some/my_file.tar.gz.zip
path1: /path/to/some/my_file.tar.gz
path2: /path/to/some/my_file.tar
path3: /path/to/some/my_file

-python3
-,

執筆者:


comment

メールアドレスが公開されることはありません。 ※ が付いている欄は必須項目です

関連記事

no image

matplotlib でエラーバーを設定する方法

pyplot.errorbar を使って設定する。 オプションの詳細は https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.erro …

no image

python3 で、リストを pop したものの返り値

リストを pop すると、 pop された値が返される。 例 plant_1 = [’pumpkin’, ‘ginger’, ‘potato’] plant_2 = [] print( …

no image

matplotlib で、凡例を表示する位置を変更する方法

凡例(legend)の表示位置を変更するには、axis の legend の loc を設定する。 次のリンクhttps://matplotlib.org/stable/api/_as_gen/mat …

no image

python3 で、dictionary のキー一覧を取得する方法

dict.keys() でキーを取得することができる。 参考:dictionary の値一覧を取得するときは、dict.values() を使う。 例 print(dict.keys())

no image

python3 で辞書(dictionary)の一部を del で削除する

辞書の一部を削除するには、del で消去したいキーを指定する。 例 dict1 = {“名前”:”太郎”, “年齢”: 20, “住所”: “東京都千代田区大手町1-1”} print(dict1) …