python os.path常用方法

版权声明: https://blog.csdn.net/t1anyuan/article/details/79806454
import os

current_file = os.path.abspath(__file__)
tmpl_dir = os.path.dirname(os.path.abspath(__file__))
tmpl_dir2 = os.path.join(os.path.dirname(os.path.abspath(__file__)),'tmp')
获取当前py文件的绝对路径

print(current_file)

获取当前py文件所在的目录

print(tmpl_dir)

当前py文件同级目录拼接

print(tmpl_dir2)

输出:

D:\我的坚果云\untitled\xxx.py
D:\我的坚果云\untitled
D:\我的坚果云\untitled\tmp

猜你喜欢

转载自blog.csdn.net/t1anyuan/article/details/79806454