diff options
author | Aaron Gordon <aaronsgordon@yahoo.com> | 2012-12-19 07:29:43 (GMT) |
---|---|---|
committer | Aaron Gordon <aaronsgordon@yahoo.com> | 2012-12-19 07:29:43 (GMT) |
commit | 421c2039856dae7b79882af5e0a50e2ab6a396cf (patch) | |
tree | c3a514046b52ea94893bae4934fe78d474926da9 /homedirectory.py |
First commit
Diffstat (limited to 'homedirectory.py')
-rw-r--r-- | homedirectory.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/homedirectory.py b/homedirectory.py new file mode 100644 index 0000000..13d83ce --- /dev/null +++ b/homedirectory.py @@ -0,0 +1,35 @@ +""" +Module for finding out the directory of the program we are running, +be it a Python script or an executable. +Use homedirectory.do() to set this path as the current os path and to +add it to sys.path. +""" + +import os +import sys + + +def _are_we_frozen(): + """Returns whether we are frozen via py2exe. + This will affect how we find out where we are located.""" + + return hasattr(sys, "frozen") + + +def our_path(): + """ This will get us the program's directory, + even if we are frozen using py2exe""" + + if _are_we_frozen(): + return os.path.dirname(unicode(sys.executable, sys.getfilesystemencoding( ))) + + return os.path.dirname(unicode(__file__, sys.getfilesystemencoding( ))) + +def do(): + """ + Sets the directory containing our program as the current directory in os, + as well as adding it to sys.path. + """ + path=our_path() + os.chdir(path) + sys.path.append(path)
\ No newline at end of file |