1.__file__
Where Module: os
Variable effect: points to the current file
The full path of the current file: os.path.abspath ( __FILE__ )
The current file Contents: os.path.dirname (os.path.abspath ( __FILE__ ))
Parent directory of the current file belongs directory: os.path.dirname ( os.path.dirname (os.path.abspath ( __FILE__ )) )
cat filelocation.py import os print(__file__) print(os.path.abspath("filelocation.py")) print(os.path.abspath(__file__)) print(os.path.dirname(os.path.abspath(__file__))) print(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 运行: filelocation.py /home/test/CodeProjects/PythonProjects/test/filelocation.py /home/test/CodeProjects/PythonProjects/test/filelocation.py /home/test/CodeProjects/PythonProjects/test /home/test/CodeProjects/PythonProjects