In [1]:
import pathlib

In [2]:
p = pathlib.Path('temp')

In [3]:
print(p)


temp

In [4]:
print(type(p))


<class 'pathlib.PosixPath'>

In [5]:
print(p.exists())


False

In [6]:
p.mkdir()

In [7]:
print(p.exists())


True

In [8]:
print(p.is_dir())


True

In [9]:
pathlib.Path('temp/dir').mkdir()

In [10]:
print(pathlib.Path('temp/dir').is_dir())


True

In [11]:
# pathlib.Path('temp/dir/sub_dir/sub_dir2').mkdir()
# FileNotFoundError: [Errno 2] No such file or directory: 'temp/dir/sub_dir/sub_dir2'

In [12]:
pathlib.Path('temp/dir/sub_dir/sub_dir2').mkdir(parents=True)

In [13]:
print(pathlib.Path('temp/dir/sub_dir/sub_dir2').is_dir())


True

In [14]:
# pathlib.Path('temp/dir').mkdir()
# FileExistsError: [Errno 17] File exists: 'temp/dir'

In [15]:
pathlib.Path('temp/dir').mkdir(exist_ok=True)

In [16]:
pathlib.Path('temp/dir/file').touch()

In [17]:
print(pathlib.Path('temp/dir/file').is_file())


True

In [18]:
# pathlib.Path('temp/dir/file').mkdir(exist_ok=True)
# FileExistsError: [Errno 17] File exists: 'temp/dir/file'

In [19]:
p_sub_dir = pathlib.Path('temp/dir/sub_dir/sub_dir2')

In [20]:
print(p_sub_dir.is_dir())


True

In [21]:
p_sub_dir.rmdir()

In [22]:
print(p_sub_dir.exists())


False

In [23]:
p = pathlib.Path('temp')

In [24]:
# p.rmdir()
# OSError: [Errno 66] Directory not empty: 'temp'

In [25]:
import shutil

In [26]:
shutil.rmtree(p)

In [27]:
print(p.exists())


False