One - One Code All

Blog Content

python获取上级模块目录-父级,祖父级,祖祖父级

Python   2014-03-11 22:19:56

父级目录的获取:只用加上os.path.dirname(当前路径)即可

import sys
import os
os.path.realpath(__file__) # 获取当前路径
os.path.dirname(os.path.realpath(__file__)) # 获取当前路径的父级路径
GRANDFA = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) # 获取父路径的父路径
sys.path.append(GRANDFA ) # 将祖父路径加入sys中

# 依次类推。


os.path.pardir是父目录,os.path.abspath是绝对路径


上一篇:python在不同层级目录import模块的方法
下一篇:numpy给矩阵元素统一赋值

The minute you think of giving up, think of the reason why you held on so long.