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

原文地址:https://www.cnblogs.com/yibeimingyue/p/14676870.html