关于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')

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

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

原文地址:https://www.cnblogs.com/ceshixuexi/p/9523972.html