pathlib

soruce

pathlib

>>> from pathlib import *
>>> p =Path(".")
>>> [x for x in p.iterdir() if x.is_dir()]
[PosixPath('test1'), PosixPath('test2')]

# glob 
>>> list(p.glob('**/*.py'))
[PosixPath('test.py'), PosixPath('test2/p1.py'), PosixPath('test2/test21/p3.py'), PosixPath('test2/test21/p2.py')]
>>> list(p.glob('*/*.py'))
[PosixPath('test2/p1.py')]

# join
>>> q = p / "test1"

# absolute()
>>> q.absolute()
PosixPath('/Users/liali/work/AI/pathlib/test1')
>>> q.resolve()
PosixPath('/Users/liali/work/AI/pathlib/test1')


>>> q.exists()
True


>>> q.is_dir()
True

# open
>>> with Path('test1/f1').open() as f: f.readline()
... 
'Hello pathlib.\n'
>>> with Path('test1/f1').open() as f: f.read()
... 
'Hello pathlib.\nHello Python.\n'


# Pure Path operation 
>>> purePath1=PurePath("test1","test11")
>>> purePath1
PurePosixPath('test1/test11')

>>> PurePosixPath('foo') == PurePosixPath('FOO')
False
>>> PureWindowsPath('foo') == PureWindowsPath('FOO')
True
>>> PureWindowsPath('FOO') in { PureWindowsPath('foo') }
True
>>> PureWindowsPath('C:') < PureWindowsPath('d:')
True

# change to str
>>> str(q)
'test1'

# resolve()
>>> str(q.resolve())
'/Users/liali/work/AI/pathlib/test1'

# parts
>>> p = PurePath('/usr/bin/python3')
>>> p.parts
('/', 'usr', 'bin', 'python3')
>>> 
>>> PurePosixPath('/etc').drive
''
>>> PurePosixPath('/etc').root
'/'
>>> PurePosixPath('/etc').anchor
'/'
>>> PurePosixPath('/etc/t1').anchor
'/'

# parents
>>> p = PureWindowsPath('c:/foo/bar/setup.py')
>>> p.parents[0]
PureWindowsPath('c:/foo/bar')
>>> p.parents[1]
PureWindowsPath('c:/foo')
>>> p.parents[2]
PureWindowsPath('c:/')

# parent
>>> p = PurePosixPath('/a/b/c/d')
>>> p.parent
PurePosixPath('/a/b/c')
>>> p.parents[0]
PurePosixPath('/a/b/c')
>>> p.parents[1]
PurePosixPath('/a/b')
>>> p.parents[2]
PurePosixPath('/a')

# name
>>> PurePosixPath('my/library/setup.py').name
'setup.py'

# suffix
>>> PurePosixPath('my/library/setup.py').suffix
'.py'

# suffixes
>>> PurePosixPath('my/library.tar.gar').suffixes
['.tar', '.gar']

# stem
>>> PurePosixPath('my/library.tar.gz').stem
'library.tar'

# as_posix()
>>> p = PureWindowsPath('c:\\windows')
>>> str(p)
'c:\\windows'
>>> p.as_posix()
'c:/windows'

# as_uri
>>> p = PurePosixPath('/etc/passwd')
>>> p.as_uri()
'file:///etc/passwd'

# is_absolute
>>> PurePosixPath('/a/b').is_absolute()
True
>>> PurePosixPath('a/b').is_absolute()
False

# is_reserved
>>> PureWindowsPath('nul').is_reserved()
True
>>> PurePosixPath('nul').is_reserved()
False

# joinpath 
>>> PurePosixPath('/etc').joinpath('init.d', 'apache2')
PurePosixPath('/etc/init.d/apache2')
>>> PurePosixPath('/etc/init.d/apache2')
PurePosixPath('/etc/init.d/apache2')

# match
>>> PurePath('a/b.py').match('*.py')
True
>>> PurePath('/a.py').match('/*.py')
True

# p.relative_to
>>> p = PurePosixPath('/etc/passwd')
>>> p.relative_to('/')
PurePosixPath('etc/passwd')
>>> p.relative_to('/etc')
PurePosixPath('passwd')

# with_name change name
>>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
>>> p.with_name('setup.py')
PureWindowsPath('c:/Downloads/setup.py')

# change suffix
>>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
>>> p.with_suffix('.bz2')
PureWindowsPath('c:/Downloads/pathlib.tar.bz2')

# file state
>>> Path.cwd()
PosixPath('/Users/liali/work/AI/pathlib')
>>> p = Path('test1/f1')
>>> p.stat().st_size
29
>>> p.stat().st_mtime
1709188931.6800501


# change mod
>>> p.stat().st_mode
33188
>>> p.chmod(0o444)
>>> p.stat().st_mode
33060
>>> 
>>> 
>>> Path('.').exists()
True
>>> Path('nonexistentfile').exists()
False

# glob
>>> sorted(Path('.').glob('*.py'))
[PosixPath('test.py')]
>>> sorted(Path('.').glob('**/*.py'))
[PosixPath('test.py'), PosixPath('test2/p1.py'), PosixPath('test2/test21/p2.py'), PosixPath('test2/test21/p3.py')]

>>> p.group()
'staff'
>>> p.is_dir()
False
>>> p.is_file()
True
>>> p.is_symlink()
False
>>> p.is_socket()
False
>>> p.is_fifo()
False
>>> p.is_block_device()
False
>>> p.is_char_device()
False

>>> p = Path()
>>> for child in p.iterdir(): child
... 
PosixPath('test1')
PosixPath('test.py')
PosixPath('test2')

Path.lchmod()
Path.lstat()
Path.mkdir(mode=0o777, parents=False)
Path.open(mode='r', buffering=-1, encoding=None, errors=None, newline=None)
Path.owner()
Path.rename(target)
Path.replace(target)
  Rename this file or directory to the given target. If target points to an existing file or directory, it will be unconditionally replaced.
Path.resolve()
  Make the path absolute, resolving any symlinks. A new path object is returned:
Path.rglob(pattern)
Path.rmdir()
  Remove this directory. The directory must be empty.
Path.symlink_to(target, target_is_directory=False)
Path.touch(mode=0o777, exist_ok=True)
Path.unlink()
  Remove this file or symbolic link. If the path points to a directory, use Path.rmdir() instead.