os.path.abspath(__file__) 作用: 獲取當前腳本的完整路徑
-
import os
-
print(os.path.abspath(__file__) )
result:
注意:
只有當在腳本中執行的時候,os.path.abspath(__file__)才會起作用,因為該命令是獲取的當前執行腳本的完整路徑,如果在交互模式或者terminate 終端中運行會報沒有__file__這個錯誤:
>>> import os >>> cur_path = os.path.dirname(os.path.abspath(__file__)) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name '__file__' is not defined
原文:https://blog.csdn.net/liuskyter/article/details/99936955