The pathlib
module handles file paths with support for different operating systems.
The example below uses pathlib to read text from a file.
>>> import pathlib
>>> pathlib.Path('myfile.txt').read_text()
'text contained in the file'
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.