測試環境
python3.7.7
正文
__file__
表示當前.py文件的路徑
os.path.dirname(__file__)
表示當前.py文件所在文件夾的路徑
os.path.dirname(os.path.dirname(__file__))
表示上級目錄
os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
則表示上上級目錄
os.path.abspath('.')
跟os.getcwd()
一樣表示當前.終端所在文件夾的路徑
os.path.abspath('..')
表示上級目錄
如有拼接其他路徑的需要可以使用os.path.abspath('.')+'/utils'
或者os.path.join
注意,__file__
在jupyter notebook中無效,在notebook中可是使用sys.path[0]
獲取當前文件夾的路徑,然後os.path.dirname(sys.path[0])
表示上級目錄