Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Correct OS path is not being displayed

I wanted to get the following path for further processes:

C:\Users\zaidd\Documents\Treasure Hunt\1

However, after the execution of the following statements I get the following path:

Code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

currentLvl = os.getcwd().split("\\")#[-1]
print(currentLvl)
path = os.getcwd()
cwd = os.path.abspath(os.path.join(path, os.pardir))
os.chdir(cwd)
print(path)
print(cwd)

Output:

['C:', 'Users', 'zaidd']
C:\Users\zaidd
C:\Users

Here is the location of my py file just in case it’s needed:

C:\Users\zaidd\Documents\Treasure Hunt\1\IT-Treasure-Hunt.py

>Solution :

It seems you just want the path to the directory containing the running python file. This is the easiest way that I know of:

import pathlib
directory = pathlib.Path(__file__).parent.resolve()

In your case, directory would end up as C:\Users\zaidd\Documents\Treasure Hunt\1

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading