我想获取文件所在的目录。例如,完整路径为:
fullpath = "/absolute/path/to/file"
# something like:
os.getdir(fullpath) # if this existed and behaved like I wanted, it would return "/absolute/path/to"
我可以这样做:
dir = '/'.join(fullpath.split('/')[:-1])
但是上面的示例依赖于特定的目录分隔符,并且不是很漂亮。有没有更好的办法?
您正在寻找这个:
>>> import os.path
>>> fullpath = '/absolute/path/to/file'
>>> os.path.dirname(fullpath)
'/absolute/path/to'
相关功能:
>>> os.path.basename(fullpath)
'file'
>>> os.path.split(fullpath)
('/absolute/path/to','file')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句