引用不同目錄/不同級目錄的python文件的路徑引導方法

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+'\\XXX')

說明:

__file__: 當前運行文件

os.path.abspath(__file__): 當前運行文件的絕對路徑

os.path.dirname(os.path.abspath(__file__)): 當前運行文件所在目錄的絕對路徑

os.path.dirname(os.path.dirname(os.path.abspath(__file__))):當前運行文件的上一級目錄的路徑

os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+'\\XXX': 當前運行文件的上一級目錄中的XXX文件目錄或文件的路徑

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+'\\XXX'):將當前運行文件的上一級目錄中的XXX文件目錄或文件的路徑填加到path

至此, 可以對XXX文件或目錄內的python文件和函數進行引用。

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章