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