“PEP 428: Jalur sistem file yang berorientasi objek modul PathLib.” Kode Jawaban

PEP 428: Jalur sistem file yang berorientasi objek modul PathLib.

>>> from pathlib import Path
Shy Skunk

PEP 428: Jalur sistem file yang berorientasi objek modul PathLib.

>>> list(p.glob('**/*.py'))
[PosixPath('test_pathlib.py'), PosixPath('setup.py'),
 PosixPath('pathlib.py'), PosixPath('docs/conf.py'),
 PosixPath('build/lib/pathlib.py')]
Shy Skunk

PEP 428: Jalur sistem file yang berorientasi objek modul PathLib.

>>> q.exists()
True
>>> q.is_dir()
False
Shy Skunk

PEP 428: Jalur sistem file yang berorientasi objek modul PathLib.

>>> PurePath()
PurePosixPath('.')
Shy Skunk

PEP 428: Jalur sistem file yang berorientasi objek modul PathLib.

>>> PurePath('setup.py')      # Running on a Unix machine
PurePosixPath('setup.py')
Shy Skunk

PEP 428: Jalur sistem file yang berorientasi objek modul PathLib.

>>> PureWindowsPath('c:/Windows', '/Program Files')
PureWindowsPath('c:/Program Files')
Shy Skunk

PEP 428: Jalur sistem file yang berorientasi objek modul PathLib.

>>> p = Path('.')
>>> [x for x in p.iterdir() if x.is_dir()]
[PosixPath('.hg'), PosixPath('docs'), PosixPath('dist'),
 PosixPath('__pycache__'), PosixPath('build')]
Shy Skunk

PEP 428: Jalur sistem file yang berorientasi objek modul PathLib.

>>> p = Path('/etc')
>>> q = p / 'init.d' / 'reboot'
>>> q
PosixPath('/etc/init.d/reboot')
>>> q.resolve()
PosixPath('/etc/rc.d/init.d/halt')
Shy Skunk

PEP 428: Jalur sistem file yang berorientasi objek modul PathLib.

>>> PurePath('foo', 'some/path', 'bar')
PurePosixPath('foo/some/path/bar')
>>> PurePath(Path('foo'), Path('bar'))
PurePosixPath('foo/bar')
Shy Skunk

PEP 428: Jalur sistem file yang berorientasi objek modul PathLib.

>>> with q.open() as f: f.readline()
...
'#!/bin/bash\n'
Shy Skunk

Jawaban yang mirip dengan “PEP 428: Jalur sistem file yang berorientasi objek modul PathLib.”

Pertanyaan yang mirip dengan “PEP 428: Jalur sistem file yang berorientasi objek modul PathLib.”

Lebih banyak jawaban terkait untuk “PEP 428: Jalur sistem file yang berorientasi objek modul PathLib.” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya