Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between the path with and without '/'? [duplicate]

I'm new to Python, and currently using the os module. I came across a doubt.

Can anyone explain me, what is the difference between the following lines?

os.mkdir('dir_name')
os.mkdir('/dir_name')

The former creates the folder in the current directory but what about the later? There's no folder created in the current directory, where is it created then?

like image 801
Nerd001 Avatar asked Feb 15 '26 00:02

Nerd001


2 Answers

os.mkdir('dir_name')  # relative

The first path is relative. The first code line will make a directory "dir_name" in the current working directory. It is relative because the path will change relative to the working directory.

os.mkdir('/dir_name')  # absolute

This second path is absolute. "/" refers the the operating system's root directory. The second code snippet will make a "dir_name" directory in the root directory. The path is absolute because unlike the "current working directory", the root directory will never change.

like image 106
Christopher Nuccio Avatar answered Feb 16 '26 14:02

Christopher Nuccio


Consider os.mkdir('../dir_name') for full picture. It is also a relative, but uses .. to denote upper level folder, relative to current one.

like image 30
Evgeny Avatar answered Feb 16 '26 13:02

Evgeny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!