转载: Discovering Python 3’s pathlib – Dante’s Python musings

The pathlib is one of the new features in Python 3 I loved immediatly when I recognized it’s advantages. The pathlib a sort of a swiss knife tool and more than capable to replace os.path because it gives object orientated programming a reason. Especially for configuration purposes the pathlib is handy:

1
2
3
4
5
>>> from pathlib import Path
>>> root = Path('../myproject')
>>> config_dir = root / Path('config')
>>> str(config_dir)
'../myproject/config'

The slash as an operator avoids brackets to join paths and makes the code very simplistic. But here doesn’t the advantage:

1
2
3
4
>>> path1 = Path('/home/dante')
>>> path2 = Path('/home/dante/project/src')
>>> path2.relative_to(path1)
PosixPath('project/src')

Of course fundamental operations like file IO and file name attributes are available:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>> config_files = config_dir.glob('*.conf))
>>> config_files
[PosixPath('root/config/network.conf'), PosixPath('root/config/project.conf')]
>>> config_files[0].unlink() # delete root/config/network.conf
>>> config_files = config_dir.glob('*.conf))
[PosixPath('root/config/project.conf')]
>>> config_files[0].stem
'project'
>>> config_files[0].suffix
'.conf'
>>> config_files[0].parent
PosixPath('root/config/)
>>> list(config_files[0].parents)
[PosixPath('root/config'), PosixPath('root'), PosixPath('.')]

Every Path object linked to a file is able to open this file:

1
2
3
4
5
>>> config_file = config_dir / Path('project.conf')
>>> with config_file.open('r') as f:
... print(f.read)
ip=127.0.0.1
>>>

But reading a file can be even shorter:

1
2
3
4
>>> config_file = config_dir / Path('project.conf')
>>> print(config_file.read_text())
ip=127.0.0.1
>>>

Python 3’s pathlib is able to supersede os.path with ease. The OOP approach makes working with this library fun.

Edited on