#navi(../)
* Pythonのスクリプト名を取得する [#l56c54ca]
Pythonスクリプトのパス情報を除いたスクリプト名のみを取得する方法を記します。~
UNIXコマンドであれば、basenameの動作と同様です。

#contents
#htmlinsertpcsp(ll-top.html,ll-sp.html)

* 動作確認環境 [#x3953840]
 $ python --version
 Python 2.7.3

 $ lsb_release -d
 Description:    Ubuntu 12.04.4 LTS

* 関連記事 [#vc4c6765]
-[[Pythonスクリプトを実行しているパス(ディレクトリ)を取得する>Python/サンプル/スクリプトを実行しているパス(ディレクトリ)を取得する]]
-[[Pythonスクリプトが存在するパス(ディレクトリ)を取得する>Python/サンプル/スクリプトが存在するパス(ディレクトリ)を取得する]]
-[[Pythonのスクリプト名を取得する>Python/サンプル/スクリプト名を取得する]]

* スクリプト名を取得する [#jfd7d8c1]
以下のサンプルコードおよび実行結果を参照してください。

** サンプルコード [#x9861a2d]
#ref(basename.py)
 import os
 
 basename = os.path.basename(__file__)
 print basename

** 実行結果 [#m40df147]
実際に実行したときの出力です。
 sakura@ubuntu:~$ python python/basename.py
 basename.py

スクリプト名のみ取得できています。

#htmlinsertpcsp(ll-btm.html,ll-sp.html)

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS