python - Extracting Directory from Path with Ending Slash -
what elegant way of extracting directory path ending slash?
for example
/foo/bar/test/
and want test.
i can os.path.basename if there no ending /.
is next best option like:
if directory[:-1] == '/': basename = os.path.basename(directory[:-1]) else: basename = os.path.basename(directory) as not os agnostic or clean.
calling os.path.abspath take care of you:
>>> import os >>> os.path.abspath('/foo/bar/test/') '/foo/bar/test' >>> os.path.abspath('/foo/bar/test') '/foo/bar/test' >>> so:
>>> os.path.basename(os.path.abspath('/foo/bar/test/')) 'test'
Comments
Post a Comment