python file read method of finishing path (path)

When using Python, the path frequently used files to read, to do a path different methods of reading way to organize:

Import os
 # os.path.dirname (__ file__) returns the .py file directory 
path1 = os.path.dirname ( __FILE__ )
 Print (path1) 

# os.path.abspath (__ file__) returns the absolute .py file path (full path) 
path2 = os.path.abspath with ( __FILE__ )
 Print (path2) 

# combination 
path3 = os.path.dirname (os.path.abspath with ( __FILE__ ))
 Print (path3) 

# the os.path.join ( ) stitching path 
Path4 = the os.path.join (os.path.dirname (os.path.abspath with ( __FILE__ )), ' 1.py ' )
 Print (Path4)

 

Guess you like

Origin www.cnblogs.com/liuyi1804/p/11610920.html