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'

Joining paths:

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

Path manipulation

python
path.name    # basename
path.stem    # basename without suffix
path.suffix  # .txt
path.parent  # PosixPath('/home/suguri')

Create a new directory:

python
path.mkdir(exist_ok=True)

Create a new file:

python
path.touch()

Iterate a directory:

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

File manipulation

ModeUseCreate fileKeep contents
rReading
wWriting
aAppending
xX-Writing-

INFO

Mode x stands for exclusive creation, and will always fail if the file already exists.

Extra modes (append to the mode above):

ModeDescription
tText mode (default).
bBinary mode.
+Open for updating (reading and writing).

Line ending (reference):

OSLine ending
Linux\n
Windows\r\n

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)

Delete file

python
path.unlink()