我想要获取当前文件的目录路径。我试过了:
>>> os.path.abspath(__file__)
'C:\\python27\\test.py'
但是如何检索目录的路径呢?
例如:
'C:\\python27\\'
发布于 2018-04-30 18:51:15
从Python3开始,推荐使用Path
:
from pathlib import Path
print("File Path:", Path(__file__).absolute())
print("Directory Path:", Path().absolute()) # Directory of current working directory, not __file__
文档:pathlib
注意:如果使用Jupyter Notebook,__file__
不会返回期望值,因此必须使用Path().absolute()
。
发布于 2010-08-07 20:24:33
import os
print os.path.dirname(__file__)
发布于 2019-05-23 17:51:37
试试这个:
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
https://stackoverflow.com/questions/3430372
复制相似问题