In [1]:
import util_make_files

util_make_files.pathlib_basic()

In [2]:
import pathlib
import os
import pprint

In [3]:
p_file = pathlib.Path('temp/file.txt')

In [4]:
print(p_file)


temp/file.txt

In [5]:
print(type(p_file))


<class 'pathlib.PosixPath'>

In [6]:
p_dir = pathlib.Path('temp/dir')

In [7]:
print(p_dir)


temp/dir

In [8]:
print(type(p_dir))


<class 'pathlib.PosixPath'>

In [9]:
print(p_file.is_file())


True

In [10]:
print(p_dir.is_file())


False

In [11]:
p_new_file = pathlib.Path('temp/new_file.txt')

In [12]:
print(p_new_file.exists())


False

In [13]:
p_new_file.touch()

In [14]:
print(p_new_file.exists())


True

In [15]:
pathlib.Path('temp/new_file2.txt').touch()

In [16]:
pprint.pprint(list(pathlib.Path('temp').iterdir()))


[PosixPath('temp/file.txt'),
 PosixPath('temp/new_file.txt'),
 PosixPath('temp/new_file2.txt'),
 PosixPath('temp/dir')]

In [17]:
p_sub_dir_file = p_dir / 'sub_dir' / 'file2.txt'

In [18]:
print(p_sub_dir_file)


temp/dir/sub_dir/file2.txt

In [19]:
print(p_sub_dir_file.is_file())


True

In [20]:
p_sub_dir_file = p_dir.joinpath('sub_dir', 'file2.txt')

In [21]:
print(p_sub_dir_file)


temp/dir/sub_dir/file2.txt

In [22]:
print(p_sub_dir_file.is_file())


True

In [23]:
p_file_join = p_dir.joinpath('..', 'file.txt')

In [24]:
print(p_file_join)


temp/dir/../file.txt

In [25]:
print(p_file)


temp/file.txt

In [26]:
print(p_file.samefile(p_file_join))


True

In [27]:
print(p_file == p_file_join)


False

In [28]:
print(p_file_join.resolve())


/Users/mbp/Documents/my-project/python-snippets/notebook/temp/file.txt

In [29]:
print(p_file.resolve())


/Users/mbp/Documents/my-project/python-snippets/notebook/temp/file.txt

In [30]:
print(p_file_join.resolve() == p_file.resolve())


True

In [31]:
print(type(p_file.resolve()))


<class 'pathlib.PosixPath'>

In [32]:
print(p_file_join.resolve().relative_to(pathlib.Path.cwd()))


temp/file.txt

In [33]:
print(p_dir.parent)


temp

In [34]:
print(p_dir.parent.joinpath('file.txt'))


temp/file.txt

In [35]:
print(p_file_join.parent)


temp/dir/..

In [36]:
print(p_file.parent)


temp

In [37]:
s = str(p_file)

In [38]:
print(s)


temp/file.txt

In [39]:
print(type(s))


<class 'str'>

In [40]:
print(os.path.isfile('temp/file.txt'))


True

In [41]:
print(os.path.isfile(p_file))


True

In [42]:
import shutil

shutil.rmtree('temp')