Python file path name
What is the difference between "./file_name"
, "../file_name"
and "file_name"
when used as a file path in Python?
For example, if you want to store in file_path, is it correct what will "../file_name"
store file_name
inside the current directory? And "./file_name"
save it to your desktop? This is really confusing.
source to share
./file_name
and file_name
mean the same - a file named file_name
in the current working directory.
../file_name
means a file named file_name
in the parent directory of the current working directory.
Summary
.
represents the current directory, while ..
represents the parent directory.
Explanation by example
if the current working directory this/that/folder
then:
-
.
leads tothis/that/folder
-
..
leads tothis/that
-
../..
leads tothis
-
.././../other
leads tothis/other
source to share
Basically, ./
this is the current directory and ../
is the parent of the current directory. Both are actually hard links on filesystems, meaning they are needed to specify relative paths.
Consider the following:
/root/
directory_a
directory_a_a
file_name
directory_a_b
file_name
directory_b
directory_b_a
directory_b_b
and consider your current working directory /root/directory_a/directory_a_a
. Then from that directory, if you link to ./file_name
, you link to /root/directory_a/directory_a_a/file_name
. On the other hand, if you link to ../file_name
, you mean /root/directory_a/file_name
.
Ultimately, ./
and ../
depend on your current working directory. If you want to be very specific, you must use an absolute path.
source to share