使用Python pathlib获取不带文件名的文件的绝对路径

Sat*_*tor 7 directory path python-3.x pathlib

我想替换file_location = os.path.abspath(os.path.dirname(__file__))pathlib来获取没有文件名的文件的绝对路径
使用 using directorypath = pathlib.Path(__file__).resolve() 给我绝对路径+文件名
我怎样才能获得没有文件名的绝对路径?

小智 11

您可以使用“.parent”: directorypath = pathlib.Path(__file__).resolve().parent
Path.parent