File Parts: Cheat Sheet
If you’ve ever said “I need to get the part of the file path that’s without the containing directory and without the suffix” and found it awkward, this cheat sheet is for you:
The terminology used here is borrowed from the Python pathlib
module:
>>> from pathlib import PurePosixPath
>>> p = PurePosixPath("/bar/baz/foo.tar.gz")
>>> p.name
'foo.tar.gz'
>>> p.parent
PurePosixPath('/bar/baz')
>>> p.suffixes
['.tar', '.gz']
>>> p.stem
'foo.tar'
>>> p.suffix
'.gz'
>>> p.root
'/'
>>> p.parent.name
'baz'
Follow me on Twitter and Facebook
Thanks to Shachar Ohana for reading drafts of this.