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.

+3


source to share


2 answers


./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 to this/that/folder

  • ..

    leads to this/that

  • ../..

    leads to this

  • .././../other

    leads to this/other

+2


source


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.

+1


source







All Articles