Python中的__file__变量

Python中的__file__变量

python中的__file__变量给出了.py文件的位置,例如在test.py

print(__file__)

打印之后,即可得到test.py的位置/home/user/test.py

常用的方法是结合os.path.abspath(os.path.dirname(__file__)),得到.py文件的所在目录的绝对路径,从而构造出同目录下另一文件的绝对路径

import os

basedir = os.path.abspath(os.path.dirname(__file__))
abs_path_to_another = (os.path.join(basedir,'another'))

猜你喜欢

转载自blog.csdn.net/luo3300612/article/details/86709608