Python Forum
Finding the .py file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Finding the .py file (/thread-5118.html)



Finding the .py file - ichabod801 - Sep-20-2017

I'm doubly confused. I'm trying to find the location of the .py file being run. I found this code on Stack Overflow:

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?


RE: Finding the .py file - snippsat - Sep-20-2017

This error comes when run it in interactive shell.
Python Shell doesn't detect current file path in __file__.
Run it in a file python my_file.py and it should work.


RE: Finding the .py file - wavic - Sep-20-2017

In addition:

location = os.path.split(os.path.abspath(__file__))[0]
print(location)



RE: Finding the .py file - snippsat - Sep-20-2017

(Sep-20-2017, 08:10 AM)wavic Wrote: In addition:
location = os.path.split(os.path.abspath(__file__))[0]
print(location)
Hmm no,it return a string path.
So [0] will only return first letter.
import os
 
loc = os.path.dirname(os.path.abspath(__file__))
print(loc)
print('-----')
loc = os.path.dirname(os.path.abspath(__file__))[0]
print(loc)
Output:
C:\1\scrape λ python loc.py C:\1\scrape ----- C



RE: Finding the .py file - wavic - Sep-20-2017

It works for me.

~ $ python /tmp/untitled.py
Output:
/tmp
OS - Arch Linux


RE: Finding the .py file - Larz60+ - Sep-20-2017

My run (interactively from cmder on windows 7 (pro))
Output:
M:\ λ python Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> location = os.path.split(os.path.abspath(__file__))[0] Traceback (most recent call last):   File "<stdin>", line 1, in <module> NameError: name '__file__' is not defined >>>



RE: Finding the .py file - snippsat - Sep-20-2017

(Sep-20-2017, 08:28 PM)wavic Wrote: It works for me.
With[0]?
I tested on Mint 18.1,and [0] works as in Windows only first letter of path.
Larz60+ Wrote:My run (interactively from cmder on windows 7 (pro))
As mention it do not work in interactive shell.


RE: Finding the .py file - Larz60+ - Sep-20-2017

Quote:As mention it do not work in interactive shell.
I would be confused if it did


RE: Finding the .py file - snippsat - Sep-20-2017

There is hack for interactive shell,add quotes. 
loc = os.path.dirname(os.path.abspath('__file__'))
I seems strange that it should work,so i tested 4 interactive shell 2 online.
All did return the correct location.


RE: Finding the .py file - Larz60+ - Sep-20-2017

Ok, that works, well I guess everything is a file,including directories,
so I guess I'm not as confused as I thought I would be.