Skip to content

Instantly share code, notes, and snippets.

@dfur
Last active November 19, 2022 06:33
Show Gist options
  • Save dfur/a485b6683399f9a2de18ca387e377044 to your computer and use it in GitHub Desktop.
Save dfur/a485b6683399f9a2de18ca387e377044 to your computer and use it in GitHub Desktop.
A brief tour of all the properties and methods in the python standard library 'pathlib' module which provides easy, object oriented file handling in modern python.

Easy object-oriented file handling in modern python

A brief tour of all the properties and methods in the pathlib module.

The pathlib module is part of the Python stardard library (as of v. 3.4) and can be used can do file handling tasks previously done by multiple libraries. Instead of using mere strings to represent filepaths, pathlib uses objects that have many useful properties and methods.

For example, pathlib can replace (amongst others):

os - .rename(), .sep(), .walk(), .chmod(), .mkdir(), .stat()
os.path - .join(), .split(), .exists(), .basename()
glob - .glob()
shutil - .move()
builtins - open(), read() write()

Starting with Python 3.6, many standard library modules now take Path() objects as well as str inputs when handling files as arguments.

Construct a Path object

with a string

Path('filepath')
--> PosixPath('filepath') on Mac or Linux
--> WindowsPath('filepath') on Windows

with multiple strings and/or Path objects

Path('/', folder', Path('another_folder'), 'file.ext')
--> Path object representing the path /folder/another_folder/file.ext

Note: on a Windows machine, a path might include a drive letter and will show backslashes
Path('c:/', folder', Path('another_folder'), 'file.ext')
--> Path object representing the path c:\folder\another_folder\file.ext

with the '/' operator

'folder' / Path(file)

with a method

Path('folder').joinpath('another_folder', Path(file))

Using Path() versus PurePath()

Generally you would use Path() which inherits the basic properties and methods of PurePath() and adds many more.

However for file path handling that does not have or require active access to the underlying filesystem you might choose to use PurePath().

Even more specifically, if you require a path suitable for a particular system you can use either PureWindowsPath() or PurePosixPath()

Path Components

Get segments of the path as strings (or sequences of strings) using the following attributes:

attributes to access file path components

Get a new modified filepath

.with_name(name) --> new filepath with original name replaced

.with_suffix(suffix) --> new filepath with the original extension replaced

.relative_to(other) --> part of path that comes after the 'other' segment

Convert filepath to other formats

.as_posix() --> with forward slashes

.as_uri() --> as a URI path to a file

Many standard library, and other, modules now accept Path objects directly for filepaths. But if you really need a string:
str(Path('folder/file.ext')) --> 'folder/file.ext'

Get characteristics of the filepath

is_absolute() --> True if it has an anchor like c:/ or just /

is_reserved() --> some special Windows thing

Concrete paths

All the above properties and methods are available for objects created using either Path() or PurePath() (or the system specific variants).

The additional methods listed below are only available for Path() objects and therefore have access to the current machine's actual filesystem.

Work on the system's files and directories

create

.mkdir() --> create a directory at this path

.touch() --> create a file at this path

.symlink_to(target) --> make this path a symbolic link to the target

delete

.rmdir() --> remove this directory (must be empty)

.unlink() --> remove this file or symbolic unlink

change

.rename(target) --> rename this file or directory to the given target (if target exists and user has permission, it will be replaced)

.replace(target) --> rename this file or directory to the given target (if target exists it will be replaced unconditionally)

.chmod(permissions) --> change the permissions (ie mode) as per os.chmod

Read and write files

.read_text() --> returns the decoded contents of the file as a strings

.read_bytes() --> return the binary contents of the file as a bytes object

.write_text(data) --> open the file, then write the data to it, and close the file (existing files replaced, non-existing files created)

.write_bytes(data) --> open the file, then write the data to it, and close the file (existing files replaced, non-existing files created)

.open(mode='r') --> open the file (in read mode by default).

Note: Unlike the previous methods, .open() should be used inside a with context manager to ensure file is closed. The previous methods include an implicit context manager.

Search and compare paths

.match(pattern) --> True if object matches the glob-style pattern

.samefile(other_path) --> True if paths point to the same file

.glob(pattern) --> yield path objects for all files matching the given pattern, possibly using shell wildcards *, ?, and []. How to use wildcards. For recursive use .rglob()

.iterdir --> yield path objects for all the contents of the given directory

Access special directories

.cwd() --> current working directory

.home() --> home directory of the current user

Make paths more explicit

.expanduser() --> new path with '~' expanded to current user's home directory

.resolve() --> new absolute path (including resolved symlinks)

Check the existance and type of a file

.exists() --> True for file or directory that exists in the filesystem

.is_dir() --> True for a directorory

.is_file() --> True for a regular file

similarly for other types
.is_symlink(), .is_socket(), .is_fifo(), .is_block_device(), .is_char_device()

Get information about files

.owner() --> name of the user owning the file

.group() --> name of the group owning the file

.stat().<attribute> --> information about the path eg uid, size, atime (see os.stat for details of attributes). Also .lstat()

Further information

Official documentation

PEP 428

RealPython tutorial

Analytics

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment