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 QR code will lead you back to this post)

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'
Discuss this post at the comment section below.
Follow me on Twitter, Facebook or Google+
Thanks to Shachar Ohana for reading drafts of this.

Similar Posts