In Python, suppose I have a path like this:
/folderA/folderB/folderC/folderD/
How can I get just the folderD part?
folderD
Use os.path.normpath, then os.path.basename:
os.path.normpath
os.path.basename
>>> os.path.basename(os.path.normpath('/folderA/folderB/folderC/folderD/')) 'folderD'
The first strips off any trailing slashes, the second gives you the last part of the path. Using only basename gives everything after the last slash, which in this case is ''.
basename
''
1.4m articles
1.4m replys
5 comments
56.9k users