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

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

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

 

相关文章:

  • 2022-12-23
  • 2021-08-23
  • 2022-12-23
  • 2021-06-27
  • 2022-12-23
  • 2022-12-23
  • 2021-07-21
  • 2021-06-12
猜你喜欢
  • 2022-12-23
  • 2021-08-10
  • 2022-12-23
  • 2021-04-02
  • 2021-09-14
  • 2022-12-23
  • 2021-05-23
相关资源
相似解决方案