There is a convenient Unipath
module.
>>> from unipath import Path
>>>
>>> Path('/var/log').exists()
True
>>> Path('/var/log').isdir()
True
Other related things you might need:
>>> Path('/var/log/system.log').parent
Path('/var/log')
>>> Path('/var/log/system.log').ancestor(2)
Path('/var')
>>> Path('/var/log/system.log').listdir()
[Path('/var/foo'), Path('/var/bar')]
>>> (Path('/var/log') + '/system.log').isfile()
True
You can install it using pip:
$ pip3 install unipath
It's similar to the built-in pathlib
. The difference is that it treats every path as a string (Path
is a subclass of the str
), so if some function expects a string, you can easily pass it a Path
object without a need to convert it to a string.
For example, this works great with Django and settings.py
:
# settings.py
BASE_DIR = Path(__file__).ancestor(2)
STATIC_ROOT = BASE_DIR + '/tmp/static'