LANGUAGE » PYTHON » PACKAGE

Pathlib

Usage

INFO

Available from version 3.4.

python
from pathlib import Path

Create a Path variable:

python
path = Path('/home/suguri/file.txt')
str(path)  # '/home/suguri/file.txt'

Path manipulation

python
path.parent    # PosixPath('/home/suguri')
path.stem      # file
path.suffix    # .txt

path1 / path2  # Same as os.path.join(path1, path2)

File manipulation

ModeDescription
rOpen for reading (default).
wOpen for writing, truncating the file first.
xOpen for exclusive creation, failing if the file already exists.
aOpen for writing, appending to the end of the file if it exists.
bBinary mode.
tText mode (default).
+Open for updating (reading and writing).

Read or write to file

python
with Path.open(path, 'r') as f:
    all_text = f.read()
    lines_list = f.readlines()

with Path.open(path, 'w') as f:
    f.write('content')

Create empty file (similar to touch)

python
with Path.open(path, 'a'):
    os.utime(path, None)

Iterate directory

python
for node in Path('.').iterdir():
    if node.is_dir():
        pass
    if node.is_file():
        pass

Delete file

python
path.unlink()