Python获取当前路径和工作路径

Python获取当前路径和工作路径

获取当前路径和工作目录,并加入到系统路径里,方便查找类、对象、函数等资源,方便从工作目录引入别的python文件中的量,例如,工作路径下,aaa/bbb/ccc.py中定义了一个ddd的类,就可以直接写成 from aaa.bbb.ccc import ddd。以下是代码。

# -*- coding: utf-8 -*-
import os
import sys 
curr_path = os.path.dirname(__file__)
sys.path.append(curr_path)
root_path = os.path.join(curr_path, "..")
sys.path.append(root_path)

print(curr_path)
print(root_path)
print(sys.path)

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注