Strava

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

 

posted @ 2020-09-13 19:33  cheflone  阅读(3998)  评论(0编辑  收藏  举报