Gavin Wiggins

AboutNotesPhotos


Pathlib

Written on November 8, 2022

The pathlib module handles file paths with support for different operating systems.

Read text

The example below uses pathlib to read text from a file.

>>> import pathlib
>>> pathlib.Path('myfile.txt').read_text()
'text contained in the file'

File name

Get the file name from a given path.

>>> import pathlib
>>> file = '/Users/gavinw/Desktop/screenshot.png'
>>> pathlib.Path(file).name
'screenshot.png'

Gavin Wiggins © 2024.
Made on a Mac with Genja. Hosted on GitHub Pages.