关于python引入文件路径的解决办法

 

若Python项目路径如此,需要在在a.py,b.py中引入FILE文件时,且无须知道绝对路径的情况下

approot.py

import os

def get_root():
    return os.path.dirname(os.path.abspath( __file__ ) )

a.py

import approot

print(approot.get_root()+'/FILE/file1.txt')

通过这种方式始终得到的是项目所在的绝对路径

当然除此之外也可以通过其他当时获得项目的根路径

 

posted @ 2018-08-23 15:21  来呀快活吧  阅读(2059)  评论(0编辑  收藏  举报
cs