python模块内置变量及其作用

Posted designera

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python模块内置变量及其作用相关的知识,希望对你有一定的参考价值。

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模块内置变量及其作用的主要内容,如果未能解决你的问题,请参考以下文章