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