Asked By – Jasonca1
If I wanted to specify a path to save files to and make directories that don’t exist in that path, is it possible to do this using the pathlib library in one line of code?
Now we will see solution for issue: Python pathlib make directories if they don’t exist
Yes, that is
From the docs:
If parents is true, any missing parents of this path are created as
needed; they are created with the default permissions without taking
mode into account (mimicking the POSIX
If parents is false (the default), a missing parent raises
If exist_ok is false (the default),
FileExistsErroris raised if the
target directory already exists.
If exist_ok is true,
FileExistsErrorexceptions will be ignored (same
behavior as the POSIX
mkdir -pcommand), but only if the last path
component is not an existing non-directory file.