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

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -