Python模块中哪些内置变量及其具体功能?
- 内容介绍
- 文章标签
- 相关推荐
本文共计224个文字,预计阅读时间需要1分钟。
1. `__file__` 所在模块:os变量作用:指向当前文件当前文件完整路径:`os.path.abspath(__file__)`当前文件所属目录:`os.path.dirname(os.path.abspath(__file__))`当前文件所属目录的上级目录:`os.path.dirname(os.path.dirname(os.path.abspath(__file__)))`
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
本文共计224个文字,预计阅读时间需要1分钟。
1. `__file__` 所在模块:os变量作用:指向当前文件当前文件完整路径:`os.path.abspath(__file__)`当前文件所属目录:`os.path.dirname(os.path.abspath(__file__))`当前文件所属目录的上级目录:`os.path.dirname(os.path.dirname(os.path.abspath(__file__)))`
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

