DekGenius.com
PYTHON
PEP 428: The pathlib module – object-oriented filesystem paths.
>>> from pathlib import Path
PEP 428: The pathlib module – object-oriented filesystem paths.
>>> p = Path('.')
>>> [x for x in p.iterdir() if x.is_dir()]
[PosixPath('.hg'), PosixPath('docs'), PosixPath('dist'),
PosixPath('__pycache__'), PosixPath('build')]
PEP 428: The pathlib module – object-oriented filesystem paths.
>>> list(p.glob('**/*.py'))
[PosixPath('test_pathlib.py'), PosixPath('setup.py'),
PosixPath('pathlib.py'), PosixPath('docs/conf.py'),
PosixPath('build/lib/pathlib.py')]
PEP 428: The pathlib module – object-oriented filesystem paths.
>>> p = Path('/etc')
>>> q = p / 'init.d' / 'reboot'
>>> q
PosixPath('/etc/init.d/reboot')
>>> q.resolve()
PosixPath('/etc/rc.d/init.d/halt')
PEP 428: The pathlib module – object-oriented filesystem paths.
>>> q.exists()
True
>>> q.is_dir()
False
PEP 428: The pathlib module – object-oriented filesystem paths.
>>> with q.open() as f: f.readline()
...
'#!/bin/bash
'
PEP 428: The pathlib module – object-oriented filesystem paths.
>>> PurePath('setup.py') # Running on a Unix machine
PurePosixPath('setup.py')
PEP 428: The pathlib module – object-oriented filesystem paths.
>>> PurePath('foo', 'some/path', 'bar')
PurePosixPath('foo/some/path/bar')
>>> PurePath(Path('foo'), Path('bar'))
PurePosixPath('foo/bar')
PEP 428: The pathlib module – object-oriented filesystem paths.
>>> PurePath()
PurePosixPath('.')
PEP 428: The pathlib module – object-oriented filesystem paths.
>>> PurePath('/etc', '/usr', 'lib64')
PurePosixPath('/usr/lib64')
>>> PureWindowsPath('c:/Windows', 'd:bar')
PureWindowsPath('d:bar')
PEP 428: The pathlib module – object-oriented filesystem paths.
>>> PureWindowsPath('c:/Windows', '/Program Files')
PureWindowsPath('c:/Program Files')
© 2022 Copyright:
DekGenius.com