2

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])
K. Shores
  • 875
  • 1
  • 18
  • 46

3 Answers3

1

This should get you the appropriate file, it will return the path of the executable, or the path of the script.

if hasattr(sys, 'frozen'):
    basis = sys.executable
else:
    basis = sys.argv[0]

if this doesnt work for you try chaning the else: section to

os.path.dirname(os.path.realpath("__file__"))

edit: obviously: import sys

Executables will add a frozen attribute, check for that attribute before checking the working directory. frozen applications will probably run against python.exe which will resolve your working directory differently

Busturdust
  • 2,447
  • 24
  • 41
  • Even with this, and with changing the `else`, I am still getting the same behavior. Hmm. I'll keep playing with this. Thank you. I will say that the command that is run by Terminal when I open the .py with terminal is `/Users/User/Desktop/Executable/filename.py ; exit;`. I don't know if that is useful or not. – K. Shores Oct 26 '15 at 18:44
  • Is that where the python file actually exists? than isnt that correct? – Busturdust Oct 26 '15 at 18:46
  • The .py file exists at `/Users/User/Desktop/Executable/filename.py`. – K. Shores Oct 26 '15 at 18:48
  • and running the executable is still giving you the wrong path? – Busturdust Oct 26 '15 at 18:48
  • Correct. Running it with `python filename.py` inside of terminal gets me the current path, but using the .py as an executable currently does not give me the correct path. – K. Shores Oct 26 '15 at 18:49
  • hmm, maybe this thread can help out http://stackoverflow.com/questions/2292703/how-can-i-get-the-executables-current-directory-in-py2exe – Busturdust Oct 26 '15 at 18:50
  • That did help, I believe I will be able to figure this out, thanks to you. I appreciate your help. – K. Shores Oct 26 '15 at 18:55
  • 1
    I got the answer. One of the dudes in my lab helped me out. The answer was to do, in the `else`, `basis = os.path.dirname(sys.argv[0])`. – K. Shores Oct 26 '15 at 19:37
0

The current working directory is not necessarily the same directory where the file actually resides. To get the path of the actual file, you could use:

os.path.dirname(os.path.realpath("__file__"))
chishaku
  • 4,577
  • 3
  • 25
  • 33
0

The documentation for os.getcwd() says: Return a string representing the current working directory. Current working directory may not be where the python script is located. You should check the documentation for a function that will give you the directory of the script, not the working directory, which may be different.

simon4ok
  • 43
  • 1
  • 7