3. pathlib

3.1. Getting filenames and extensions

3.1.1. Extensions

import pathlib

pathlib.Path('my/library/setup.py').suffix   # '.py'
pathlib.Path('my/library.tar.gz').suffix     # '.gz'
pathlib.Path('my/library').suffix            # ''
pathlib.Path('my/library.tar.gar').suffixes  # ['.tar', '.gar']
pathlib.Path('my/library.tar.gz').suffixes   # ['.tar', '.gz']
pathlib.Path('my/library').suffixes          # []

3.1.2. Filenames

import pathlib

pathlib.Path('//some/share/setup.py').name  # 'setup.py'
pathlib.Path('//some/share').name           # ''
pathlib.Path('my/library.tar.gz').stem      # 'library.tar'
pathlib.Path('my/library.tar').stem         # 'library'
pathlib.Path('my/library').stem             # 'library'

3.2. System os vs. pathlib

Table 54. System os vs. pathlib

os and os.path

pathlib

os.path.abspath()

Path.resolve()

os.getcwd()

Path.cwd()

os.path.exists()

Path.exists()

os.path.expanduser()

Path.expanduser() and Path.home()

os.path.isdir()

Path.is_dir()

os.path.isfile()

Path.is_file()

os.path.islink()

Path.is_symlink()

os.stat()

Path.stat(), Path.owner(), Path.group()

os.path.isabs()

PurePath.is_absolute()

os.path.join()

PurePath.joinpath()

os.path.basename()

PurePath.name

os.path.dirname()

PurePath.parent

os.path.splitext()

PurePath.suffix

3.3. .home()

import pathlib

pathlib.home()  # WindowsPath('C:/Users/José')

3.4. .drive

import pathlib

PureWindowsPath('c:/Program Files/').drive  # 'c:'
PureWindowsPath('/Program Files/').drive    # ''
PurePosixPath('/etc').drive                 # ''

3.5. .parents

import pathlib

p = PureWindowsPath('c:/foo/bar/setup.py')

p.parents[0]    # PureWindowsPath('c:/foo/bar')
p.parents[1]    # PureWindowsPath('c:/foo')
p.parents[2]    # PureWindowsPath('c:/')

3.6. .parent

import pathlib

p = PurePosixPath('/a/b/c/d')
p.parent        # PurePosixPath('/a/b/c')

3.7. .as_posix()

import pathlib

p = PureWindowsPath('c:\\windows')

str(p)          # 'c:\\windows'
p.as_posix()    # 'c:/windows'

3.8. .as_uri()

import pathlib

p = PurePosixPath('/etc/passwd')
p.as_uri()      # 'file:///etc/passwd'

p = PureWindowsPath('c:/Windows')
p.as_uri()      # 'file:///c:/Windows'

3.9. Path.chmod()

import pathlib

p = Path('setup.py')

oct(p.stat().st_mode)  # 0o100775
p.chmod(0o444)
oct(p.stat().st_mode)  # 0o100444

3.10. .glob()

import pathlib

sorted(Path('.').glob('*.py'))
# [PosixPath('pathlib.py'), PosixPath('setup.py'), PosixPath('test_pathlib.py')]

sorted(Path('.').glob('*/*.py'))
# [PosixPath('docs/conf.py')]

sorted(Path('.').glob('**/*.py'))
# [PosixPath('docs/conf.py'), ...]

3.11. .iterdir()

import pathlib

p = Path('docs')

for child in p.iterdir():
    print(child)

# PosixPath('docs/conf.py')
# PosixPath('docs/index.rst')
# PosixPath('docs/Makefile')
# PosixPath('docs/_build')
# PosixPath('docs/_static')
# PosixPath('docs/_templates')

3.12. joining paths

from pathlib import Path

directory = Path("/etc")
filepath = directory / "my_file.txt"

if filepath.exists():
    print('ok')