pathlib.Path.write_text()

Path.write_text(data, encoding=None, errors=None) Open the file pointed to in text mode, write data to it, and close the file: >>> p = Path('my_text_file') >>> p.write_text('Text file contents') 18 >>> p.read_text() 'Text file contents' New in version 3.5.

pathlib.Path.write_bytes()

Path.write_bytes(data) Open the file pointed to in bytes mode, write data to it, and close the file: >>> p = Path('my_binary_file') >>> p.write_bytes(b'Binary file contents') 20 >>> p.read_bytes() b'Binary file contents' An existing file of the same name is overwritten. New in version 3.5.

pathlib.Path.unlink()

Path.unlink() Remove this file or symbolic link. If the path points to a directory, use Path.rmdir() instead.

pathlib.Path.touch()

Path.touch(mode=0o666, exist_ok=True) Create a file at this given path. If mode is given, it is combined with the process’ umask value to determine the file mode and access flags. If the file already exists, the function succeeds if exist_ok is true (and its modification time is updated to the current time), otherwise FileExistsError is raised.

pathlib.Path.symlink_to()

Path.symlink_to(target, target_is_directory=False) Make this path a symbolic link to target. Under Windows, target_is_directory must be true (default False) if the link’s target is a directory. Under POSIX, target_is_directory‘s value is ignored. >>> p = Path('mylink') >>> p.symlink_to('setup.py') >>> p.resolve() PosixPath('/home/antoine/pathlib/setup.py') >>> p.stat().st_size 956 >>> p.lstat().st_size 8 Note The order of arguments (link, target

pathlib.Path.stat()

Path.stat() Return information about this path (similarly to os.stat()). The result is looked up at each call to this method. >>> p = Path('setup.py') >>> p.stat().st_size 956 >>> p.stat().st_mtime 1327883547.852554

pathlib.Path.samefile()

Path.samefile(other_path) Return whether this path points to the same file as other_path, which can be either a Path object, or a string. The semantics are similar to os.path.samefile() and os.path.samestat(). An OSError can be raised if either file cannot be accessed for some reason. >>> p = Path('spam') >>> q = Path('eggs') >>> p.samefile(q) False >>> p.samefile('spam') True New in version 3.5.

pathlib.Path.rmdir()

Path.rmdir() Remove this directory. The directory must be empty.

pathlib.Path.rglob()

Path.rglob(pattern) This is like calling glob() with “**” added in front of the given pattern: >>> sorted(Path().rglob("*.py")) [PosixPath('build/lib/pathlib.py'), PosixPath('docs/conf.py'), PosixPath('pathlib.py'), PosixPath('setup.py'), PosixPath('test_pathlib.py')]

pathlib.Path.resolve()

Path.resolve() Make the path absolute, resolving any symlinks. A new path object is returned: >>> p = Path() >>> p PosixPath('.') >>> p.resolve() PosixPath('/home/antoine/pathlib') ”..” components are also eliminated (this is the only method to do so): >>> p = Path('docs/../setup.py') >>> p.resolve() PosixPath('/home/antoine/pathlib/setup.py') If the path doesn’t exist, FileNotFoundError is raised. If an infinite loop is encountered along the re