so I am trying to make an executable for one of my python programs. I need to have access to the files in the folder that my executable is located. Because this file might get moved around, I am using import os; current_dir = os.getcwd().
When I run the program through terminal with python filename.py and I print current_dir, I get /Users/User/Desktop/Executable/.
When I run the executable by opening the .py with terminal through Finder (I am on OS X), and the current_dir is printed, I get /Users/User. Notice, this does not have the other 2 levels that I need, namely Desktop/Executable. Why might this happen?
Remark: This is just a one off, quick executable that isn't meant to be used for a long time, but is necessary, so making the .py executable serves our purpose. This was done by chmod +x filename.py.
The answer, which can be found following the comments of the chosen answer to this question is:
import os
import sys
if hasattr(sys, 'frozen'):
basis = sys.executable
else:
basis = os.path.dirname(sys.argv[0])