#navi(../)
* Pythonスクリプトが存在するパス(ディレクトリ)を取得する [#pcfeea33]
Pythonスクリプトが存在するパス(ディレクトリ)を取得するサンプルコードを以下に説明します。
#contents
#htmlinsertpcsp(ll-top.html,ll-sp.html)
* 動作確認環境 [#vd71554f]
$ python --version
Python 2.7.3
$ lsb_release -d
Description: Ubuntu 12.04.4 LTS
* 関連記事 [#ca1e54c2]
-[[Pythonスクリプトを実行しているパス(ディレクトリ)を取得する>Python/サンプル/スクリプトを実行しているパス(ディレクトリ)を取得する]]
-[[Pythonスクリプトが存在するパス(ディレクトリ)を取得する>Python/サンプル/スクリプトが存在するパス(ディレクトリ)を取得する]]
-[[Pythonのスクリプト名を取得する>Python/サンプル/スクリプト名を取得する]]
* スクリプト存在するパス(ディレクトリ)を取得する [#fba119f1]
以下のサンプルコードおよび実行結果を参照してください。
** サンプルコード [#j49cd977]
#ref(path.py)
import os
d = os.path.abspath(__file__)
print d
** 実行結果 [#bd141f13]
今回実行するPythonのサンプルコードは以下のディレクトリにあります。
sakura@ubuntu:~$ pwd
/home/sakura
sakura@ubuntu:~$ ls -l python/path.py
-rw-rw-r-- 1 sakura sakura 49 2月 24 15:36 python/path.py
実際に実行したときの出力です。
sakura@ubuntu:~$ python python/path.py
/home/sakura/python/path.py
スクリプトが存在する絶対パスが表示されているのが確認できます。
以上、Pythonスクリプトの絶対パスを取得する方法でした。
#htmlinsertpcsp(ll-btm.html,ll-sp.html)