0

I'm very new to programming, and to Python. I'm on Mac OSX, trying to work with PyCharm. I've looked at this, this, this, and several more.

But I can't seem to get the path of the current running file. If I use os.getcwd(), I get

'/Users/AlanH/Python'

When really, the full file path is:

'/Users/AlanH/Python/Exercises/PythonBasics/starthere.py'

So I don't understand why that doesn't work.

If I try using sys.path[0], it prings up a path that takes me to my Library, then dives in to Enthought (don't know why, even though I'm using Anaconda distribution).

I could go on and on about all the possible solutions I've tried, but nothing works. All I want is to get the exact path up to current running file. So either

'/Users/AlanH/Python/Exercises/PythonBasics/starthere.py'

or this

'/Users/AlanH/Python/Exercises/PythonBasics'

will do.

Could someone please help?

Community
  • 1
  • 1
TheRealFakeNews
  • 7,512
  • 16
  • 73
  • 114

1 Answers1

1
import os
print(os.path.abspath(__file__))

Locally tested with the following result:

eb@cube ~/Share $ python stackoverflow/path.py
/home/eb/Share/stackoverflow/path.py
Eirik Birkeland
  • 588
  • 5
  • 11