Skip to content

Python pathlib Package: Overview and Examples

The pathlib package in Python offers a convenient and powerful way to handle filesystem paths. It provides an object-oriented interface for working with paths and is part of the standard library from Python 3.4 onwards.

Importing the pathlib Module

To use pathlib, you need to import it.

from pathlib import Path

Creating Paths

Creating Path Objects

You can create a Path object using the Path class.

p = Path('some_directory/some_file.txt')
print(p)

Current Directory

To get the current working directory, use Path.cwd().

current_directory = Path.cwd()
print(current_directory)

Home Directory

To get the home directory, use Path.home().

home_directory = Path.home()
print(home_directory)

Common Path Operations

Joining Paths

You can join paths using the / operator.

p = Path('some_directory') / 'some_file.txt'
print(p)

Checking Path Existence

You can check if a path exists using the exists() method.

p = Path('some_directory/some_file.txt')
print(p.exists())

Checking if Path is a File or Directory

Use the is_file() and is_dir() methods to check if the path is a file or a directory.

print(p.is_file())
print(p.is_dir())

Creating Directories

Use the mkdir() method to create a directory.

p = Path('new_directory')
p.mkdir()

To create parent directories as needed, use the parents argument.

p = Path('parent_directory/child_directory')
p.mkdir(parents=True)

Removing Files and Directories

Use the unlink() method to remove a file and rmdir() to remove a directory.

p = Path('new_directory/some_file.txt')
p.unlink()

p = Path('new_directory')
p.rmdir()

Iterating Over Directory Contents

Use the iterdir() method to iterate over the contents of a directory.

p = Path('.')
for item in p.iterdir():
    print(item)

Reading and Writing Files

Reading a File

Use the read_text() method to read the contents of a file as a string.

p = Path('example_file.txt')
content = p.read_text()
print(content)

Writing to a File

Use the write_text() method to write a string to a file.

p = Path('example_file.txt')
p.write_text('Hello, world!')

Path Properties and Methods

Getting the Name, Stem, and Suffix

Use the name, stem, and suffix properties to get the name, stem, and suffix of a file.

p = Path('some_directory/some_file.txt')
print(p.name)    # Output: some_file.txt
print(p.stem)    # Output: some_file
print(p.suffix)  # Output: .txt

Getting the Parent Directory

Use the parent property to get the parent directory.

p = Path('some_directory/some_file.txt')
print(p.parent)  # Output: some_directory

Resolving Paths

Use the resolve() method to get the absolute path.

p = Path('some_directory/some_file.txt')
print(p.resolve())

Example: Creating a Directory, Writing a File, and Cleaning Up

from pathlib import Path

# Create a new directory
p = Path('example_dir')
p.mkdir()

# Create and write to a new file
file_path = p / 'example_file.txt'
file_path.write_text('Hello, world!')

# Read the file
content = file_path.read_text()
print(content)

# Remove the file and directory
file_path.unlink()
p.rmdir()

Conclusion

The pathlib package in Python provides a modern and intuitive way to work with filesystem paths. It offers a rich set of methods and properties to perform common path operations, making it a powerful alternative to the older os.path module. Understanding and utilizing pathlib can significantly simplify your file and directory handling tasks in Python.