os.path.basename returns the final component of a filesystem pathname—the file or directory name at the end. section 12.2.4
Extract a filename
import os os.path.basename(os.path.join('some', 'directory', 'path.jpg')) # 'path.jpg' section 12.2.4
Related pathname operations separate complementary parts:
- os.path.split returns the directory portion and basename as a two-element tuple. section 12.2.4
- os.path.dirname returns the path before the final component. section 12.2.4
- os.path.splitext separates a dotted extension from the rest of the pathname. section 12.2.4
import os
os.path.basename(os.path.join('some', 'directory', 'path.jpg'))
'path.jpg'
os.path.dirname(os.path.join('some', 'directory', 'path.jpg'))
'some/directory'
Compare basename extraction with dirname using the same constructed pathname. section 12.2.4
