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

how to get the relative path of a jar file from outside in python

I have a project structure like the one below:

└── core
    ├── lib
    │   └── some.jar
    └── src
        └── main
            └── main.py

Now I am trying to access the some.jar inside the main.py file. It works fine with the absolute path but when I try to do something like below, it fails.

file:///../../lib/some.jar  

I also tried the below 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

os.path.join(os.path.dirname(os.path.realpath(__file__)), "lib")  

but It gives the path as below:

/Users/IdeaProjects/getmesomeproject/core/src/main/lib

Is there anyway to pass the relative path of the JAR file?

>Solution :

You can use pathlib to get the parent path

from pathlib import Path

os.path.join(Path(os.path.dirname(os.path.realpath(__file__))).parent.parent, "lib")

This should give you

/Users/IdeaProjects/RankedMatchmaking/core/lib
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