os.path.abspath(__file__)用法及意義


os.path.abspath(__file__) 作用: 獲取當前腳本的完整路徑
  1.  
    import os
  2.  
    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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM