Python reads file relative path understanding and file read path format

Absolute path : refers to the specific location where the current file is stored in the computer disk, it is dead, the physical one

Relative path : refers to the location of the file relative to the current py file . It is to refer to the py file to explain the path, the reference object

Way to read file path (path.xls file is an example)

"D:\College\python\analyse\Assignment 1\path.xls" reads the absolute path at this time

 

The py file that executes the command is in the same directory as the path.xls file, and the relative path is as follows

path = r" Job 1\path.xls " #relative path                        

If it is in the same directory path = r" path.xls ", it is ok to write directly

path = r" ../path2.xls " corresponds to the file path2.xls " ../file  " in the upper directory of the path.py file

 

File paths under Windows are separated by "\".
When the python program imports data files, since the backslash "\" in the python language is an escape character, such as "\n" means carriage return, "\t" means tab, etc., if you continue to use "\" to represent files path, there will be ambiguity.

The solution is as follows:

Use the slash "/", such as "C:/test.py",
use the backslash symbol to escape, such as "C:\\test.py"
, use python's raw string, which means no escaping, such as r"C :\test.py"

 

Guess you like

Origin blog.csdn.net/JLwwfs/article/details/129276897