python模块内置变量及其作用
发布时间:2020-12-20 10:25:59 所属栏目:Python 来源:网络整理
导读:1. __file__ 所在模块:os 变量作用:指向当前文件 当前文件的完整路径:os.path.abspath( __file__ ) 当前文件所属目录:os.path.dirname(os.path.abspath( __file__ )) 当前文件所属目录的上级目录:os.path.dirname( os.path.dirname(os.path.abspath( __
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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |