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

python でファイルを1行おきに読み込む方法

readline を使って次のように書く。 例 ファイル:data.txt # id name age 1 佐藤太郎 10 2 鈴木花子 18 3 坂本明美 21 4 松村光子 24 5 小川奏子 1 …

no image

matplotlib で複数のグラフを並べて表示する方法

plot.subplots() でグラフの縦方向と横方向の数を指定する。 例 import numpy as np from matplotlib import pyplot as plt x1 = …

no image

matplotlib で、縦横の罫線を引く方法

破線をカスタマイズする方法 以下では、dashes = [5,2,2,2] で、線5・空白2・線2・空白2の破線を指定している。 詳しくは次を参照。 https://matplotlib.org/st …

no image

matplotlib でグラフの背景の色を変える方法(facecolor)

次のように、axes で facecolor を変更すればよい。 例 from matplotlib import pyplot as plt x = [2,7,8] y = [7,1 …