首页 > 编程语言 > 详细

python模块内置变量及其作用

时间:2019-10-02 23:10:06      阅读:296      评论:0      收藏:0      [点我收藏+]

1.__file__

所在模块:os

变量作用:指向当前文件

当前文件的完整路径:os.path.abspath(__file__)

当前文件所属目录:os.path.dirname(os.path.abspath(__file__))

当前文件所属目录的上级目录:os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

 

cat filelocation.py
import os
print(__file__)
print(os.path.abspath("filelocation.py"))
print(os.path.abspath(__file__))
print(os.path.dirname(os.path.abspath(__file__)))
print(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
运行:
filelocation.py
/home/test/CodeProjects/PythonProjects/test/filelocation.py
/home/test/CodeProjects/PythonProjects/test/filelocation.py
/home/test/CodeProjects/PythonProjects/test
/home/test/CodeProjects/PythonProjects

 

 

 

 

python模块内置变量及其作用

原文:https://www.cnblogs.com/DesignerA/p/11618601.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!