3

What is the difference between "./file_name", "../file_name" and "file_name"when used as the file path in Python?

For example, if you want to save in the file_path, is it correct that "../file_name" will save file_name inside the current directory? And "./file_name" will save it to the desktop? It's really confusing.

0

2 Answers 2

3

./file_name and file_name mean the same thing - a file called file_name in the current working directory.

../file_name means a file called file_name in the in the parent directory of the current working directory.

Summary

. represents current directory whereas .. represents parent directory.

Explanation by example

if the current working directory is this/that/folder then:

  • . results in this/that/folder
  • .. results in this/that
  • ../.. results in this
  • .././../other results in this/other
Sign up to request clarification or add additional context in comments.

7 Comments

What do you mean by parent directory of the current working directory?
What if my current directory is the Desktop?
I think I got it. My Desktop folder is located in my asus folder which is why files are being saved there. Am I correct?
Usually, your current working directory is the directory in which you ran the python file. To find out your current working directory during execution do this import os and print(os.cwd())
I have another question. what does '.' mean? It was being used for the log_dir arg in the tf.train.write_graph() function
|
1

Basically, ./ is the current directory, while ../ is the parent of the current directory. Both are actually hard links in filesystems, i.e., they are needed in order to specify relative paths.

Let's 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 let's consider your current working directory is /root/directory_a/directory_a_a. Then, from this directory if you refer to ./file_name you are referring to /root/directory_a/directory_a_a/file_name. On the other hand, if you refer to ../file_name you are referring to /root/directory_a/file_name.

In the end, ./ and ../ depend upon your current working directory. If you want to be very specific you should use an absolute path.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.