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


Messages In This Thread
Finding the .py file - by ichabod801 - Sep-20-2017, 02:04 AM
RE: Finding the .py file - by snippsat - Sep-20-2017, 03:21 AM
RE: Finding the .py file - by wavic - Sep-20-2017, 08:10 AM
RE: Finding the .py file - by snippsat - Sep-20-2017, 12:58 PM
RE: Finding the .py file - by wavic - Sep-20-2017, 08:28 PM
RE: Finding the .py file - by Larz60+ - Sep-20-2017, 08:57 PM
RE: Finding the .py file - by snippsat - Sep-20-2017, 09:50 PM
RE: Finding the .py file - by Larz60+ - Sep-20-2017, 11:00 PM
RE: Finding the .py file - by snippsat - Sep-20-2017, 11:23 PM
RE: Finding the .py file - by Larz60+ - Sep-20-2017, 11:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding Duplicate in CSV file bond009 3 2,400 May-14-2020, 05:37 AM
Last Post: bond009
  capture next block of text after finding error in file kdefilip2 6 3,496 Nov-27-2019, 07:36 PM
Last Post: buran
  Finding a specific line in a file Vqlk 3 2,624 Sep-07-2019, 08:20 PM
Last Post: Axel_Erfurt
  finding own python source file to read it Skaperen 3 3,049 Aug-27-2018, 11:23 PM
Last Post: Skaperen
  Finding a number in a file printing out number and next word petertyler 2 2,573 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