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.
Path('filepath')
--> PosixPath('filepath')
on Mac or Linux
--> WindowsPath('filepath')
on Windows
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
'folder' / Path(file)
Path('folder').joinpath('another_folder', Path(file))
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()
Get segments of the path as strings (or sequences of strings) using the following attributes:
.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
.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'
is_absolute()
--> True
if it has an anchor like c:/
or just /
is_reserved()
--> some special Windows thing
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.
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_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.
.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
.cwd()
--> current working directory
.home()
--> home directory of the current user
.expanduser()
--> new path with '~' expanded to current user's home directory
.resolve()
--> new absolute path (including resolved symlinks)
.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()
.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()