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


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
    >>> 

 


————————————————
版權聲明:本文為CSDN博主「liuskyter」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/liuskyter/article/details/99936955


免責聲明!

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



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