Relative and absolute path in python

let's learn about relative and absolute path in python. wait, what's that? here's an example,

C:\Users\user01\python\example.py - Absolute Path

and

python\example.py - Relative path

Absolute path is like defining the exact location of the specific file, and relative path is like defining the location of the file with respect to another file, in this case a python file. Now let's see an example.

we have 2 files in C:\Users\user01\python\example.py and C:\Users\user01\python\test_text.txt.

We gave the absolute path of the both files. but this is a tedious work if you have many such folders in your computer.

when you realise C:\Users\user01\python\ is common in both files, so if you need to open test_text.txt using python. you can either give C:\Users\user01\python\test_text.txt or test_text.py. because the python file is inC:\Users\user01\python\ the file knows it is there, you don't need to specify it again.

it is same like going to a person and telling your home is in Number-100/st street/abc colony/chennai/tamil nadu/india/earth/solar system/milky way galaxy.

you don't need that extra information of earth/solar system/milky way galaxy because you both are in earth (unless you are talking with a alien).

it's the same thing with computers too.

No Comments Yet