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

pythonで配列(リスト)の、ある要素がわかっているときにその次の要素を取得する方法。

リストを iter に変えたあと、… next() を使う。 参考リンク https://www.programiz.com/python-programming/methods/buil …

no image

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

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

no image

matplotlib でグラフの中に注釈の文字を書く方法

annotate を使う。 例 import numpy as np from matplotlib import pyplot as plt x = np.linspace(-10,10,100) …

no image

python3 で、すべて小文字かどうかを判定する方法

islower() 関数を使う。 例 print(‘abcde’.islower()) print(‘abcDe’.islower()) 結果 True False

no image

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

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