Pathlib Cheat Sheet

chris48s

chris48s

Posted on May 6, 2021

Pathlib Cheat Sheet
>>> from pathlib import Path
>>> f = Path.home() / 'foo.tar.gz'
# same as f = Path('/home/chris/foo.tar.gz')

>>> f.is_file()
True

>>> f.is_dir()
False

>>> f.exists()
True

>>> f.absolute()
PosixPath('/home/chris/foo.tar.gz')

>>> f.as_uri()
'file:///home/chris/foo.tar.gz'

>>> f.as_posix()
'/home/chris/foo.tar.gz'

>>> f.parts
('/', 'home', 'chris', 'foo.tar.gz')

>>> f.suffix
'.gz'

>>> f.suffixes
['.tar', '.gz']

>>> f.parent
PosixPath('/home/chris')
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
chris48s
chris48s

Posted on May 6, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related