Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Absolute Path vs Relative Path in Python

For reference. The absolute path is the full path to some place on your computer. The relative path is the path to some file with respect to your current working directory (PWD). For example:

Absolute path: C:/users/admin/docs/stuff.txt

If my PWD is C:/users/admin/, then the relative path to stuff.txt would be: docs/stuff.txt

Note, PWD + relative path = absolute path.

Cool, awesome. Now, I wrote some scripts which check if a file exists.

os.chdir("C:/users/admin/docs") os.path.exists("stuff.txt")

This returns TRUE if stuff.txt exists and it works.

Now, instead if I write,

os.path.exists("C:/users/admin/docs/stuff.txt")

This also returns TRUE.

Is there a definite time when we need to use one over the other? Is there a methodology for how python looks for paths? Does it try one first then the other?

Thanks!

like image 814
samGon Avatar asked Jun 27 '17 03:06

samGon


People also ask

When would you use a relative path vs an absolute path?

In simple words, an absolute path refers to the same location in a file system relative to the root directory, whereas a relative path points to a specific location in a file system relative to the current directory you are working on.

Why is it better to use relative paths instead of absolute paths?

Relative links show the path to the file or refer to the file itself. A relative URL is useful within a site to transfer a user from point to point within the same domain. Absolute links are good when you want to send the user to a page that is outside of your server.

What is the difference between absolute path and relative path in Python?

An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path is defined as the path related to the present working directly(pwd).

Why do we use absolute paths?

An absolute path makes no assumptions about your current location in relation to the location of the file or directory it's describing. An absolute path always begins from the absolute start of your hard drive and describes every step you must take through the filesystem to end up at the target location.


2 Answers

If you don't know where the user will be executing the script from, it is best to compute the absolute path on the user's system using os and __file__.

__file__ is a global variable set on every Python script that returns the relative path to the *.py file that contains it.

import os
my_absolute_dirpath = os.path.abspath(os.path.dirname(__file__))
like image 122
Chris Conlan Avatar answered Sep 20 '22 22:09

Chris Conlan


The biggest consideration is probably portability. If you move your code to a different computer and you need to access some other file, where will that other file be? If it will be in the same location relative to your program, use a relative address. If it will be in the same absolute location, use an absolute address.

like image 27
Liam Bohl Avatar answered Sep 20 '22 22:09

Liam Bohl