Reputation: 9538
This is my current folder structure, and i need to get the absolute path to the root folder
.
└── root/
├── api.py
└── programexecutablefolder/
└── mainentry.py
The program has to start at mainentry.py, however I need to get the path of the root folder from api.py (though the entry to api.py is through mainentry.py)
Also os.getcwd()
doesn't work as it will get the directory of mainentry.py
Upvotes: 0
Views: 378
Reputation: 143274
Modules (typically) have an attribute named __file__
that contains the name of the file the module was loaded from, so you should be able to do something along the lines of:
os.path.dirname(api.__file__)
(BTW: os.getcwd()
is not guaranteed to give you the directory of mainentry.py unless that's the directory you're in when you start your script.)
Upvotes: 1