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

Absolute vs. Relative File Path (Python)

Is it better to use a relative or absolute file path in Python, in both a situational setting and for best practice?

I know what both of them do, and I’m wondering if, for example, always using an absolute file path is better practice than using a relative file path, or if it depends.

And if it is a situational thing, when should you use one or the other?

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

>Solution :

For best practice, I would strongly suggest relative paths. Especially if you’re going to distribute your Python project across different systems.

The core problem with the absolute path is that it’ll break the code if someone else tries to run the code containing your or the original author’s absolute path.

Pathlib can help you with paths in general.

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