Last active
August 29, 2015 14:21
-
-
Save sherbang/13007d7f9460e7181b99 to your computer and use it in GitHub Desktop.
Find program directory for running python program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
def get_app_directory(): | |
"""Return the directory that contains the application. | |
This is the directory of the .py file in most cases, but is the directory | |
of the .exe file in the case of py2exe. | |
""" | |
import imp | |
if (hasattr(sys, "frozen") # new py2exe | |
or hasattr(sys, "importers") # old py2exe | |
or imp.is_frozen("__main__")): # tools/freeze | |
return os.path.abspath(os.path.dirname(sys.executable)) | |
else: | |
import __main__ | |
if hasattr(__main__, '__file__'): | |
return os.path.abspath(os.path.dirname(__main__.__file__)) | |
else: | |
return os.path.abspath(os.path.dirname(sys.argv[0])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment