Sep-20-2017, 02:04 AM
I'm doubly confused. I'm trying to find the location of the .py file being run. I found this code on Stack Overflow:
The reason I am looking into this is that I am writing a package that is going to store user information. I want to store it in a specific place, because I want you to have access to that same user information from wherever you run the package. And I want it to work on all of the major OS's. Currently I am storing it using relative paths from the package itself, so it stores in the same folder. But that doesn't work if you import the package while in another part of the file system. That's why I was looking at the code above, so I could always get the location of the package and store it with the package.
But I'm not sure storing it with the package is a good way to do it. It seems the OS's have different places the expect programs to store files with user data. Is it okay to store it with the package, or should I be trying to use the OS specific locations? How would I find the OS specific locations?
import os loc = os.path.dirname(os.path.abspath(__file__)) print(loc)Here's something that happened when I was testing this out:
Output:craig@craig-Latitude-E5440:~/Documents/Python$ python test.py
/home/craig/Documents/Python
craig@craig-Latitude-E5440:~/Documents/Python$ python -i test.py
/home/craig/Documents/Python
>>> __file__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '__file__' is not defined
How can __file__ not be defined if it was used to print the line previous to me typing it in?The reason I am looking into this is that I am writing a package that is going to store user information. I want to store it in a specific place, because I want you to have access to that same user information from wherever you run the package. And I want it to work on all of the major OS's. Currently I am storing it using relative paths from the package itself, so it stores in the same folder. But that doesn't work if you import the package while in another part of the file system. That's why I was looking at the code above, so I could always get the location of the package and store it with the package.
But I'm not sure storing it with the package is a good way to do it. It seems the OS's have different places the expect programs to store files with user data. Is it okay to store it with the package, or should I be trying to use the OS specific locations? How would I find the OS specific locations?