Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding the .py file
#1
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?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#2
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.
Reply
#3
In addition:

location = os.path.split(os.path.abspath(__file__))[0]
print(location)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
(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
Reply
#5
It works for me.

~ $ python /tmp/untitled.py
Output:
/tmp
OS - Arch Linux
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
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 >>>
Reply
#7
(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.
Reply
#8
Quote:As mention it do not work in interactive shell.
I would be confused if it did
Reply
#9
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.
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding Duplicate in CSV file bond009 3 2,381 May-14-2020, 05:37 AM
Last Post: bond009
  capture next block of text after finding error in file kdefilip2 6 3,439 Nov-27-2019, 07:36 PM
Last Post: buran
  Finding a specific line in a file Vqlk 3 2,595 Sep-07-2019, 08:20 PM
Last Post: Axel_Erfurt
  finding own python source file to read it Skaperen 3 3,016 Aug-27-2018, 11:23 PM
Last Post: Skaperen
  Finding a number in a file printing out number and next word petertyler 2 2,547 May-10-2018, 09:09 AM
Last Post: petertyler

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020