没有基本名称的文件的路径


75

如何获取没有文件基名的文件的路径?

就像是 /a/path/to/my/file.txt->/a/path/to/my/

尝试使用.split()失败

Answers:



8

您可以 import os

>>> filepath
'/a/path/to/my/file.txt'
>>> os.path.dirname(filepath)
'/a/path/to/my'
>>> 


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.