Analysis on the choice of open file path in Python


1、 Problem description

When you use open in Python to open a file for writing, you often find that the file to be written is not in the same level directory. In this way, you need to find and open the file according to the path of the file.

However, sometimes the absolute path and relative path will fail to open if they are written incorrectly.

2、 Solution

1. Use absolute path

Error method:


If it represents the windows file path, the use of diagonal bar / and backslash \ is the same; If you represent a network file path, you must use a diagonal bar /

But I don’t like using absolute paths very much. I recommend using relative paths.

2. Use relative path

Error method:


Because it is in a non peer directory, it needs to be added before the file directory/

This is the end of this article on the analysis of the choice of open file path in Python. For more information about Python open file path, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!