Renaming and Deleting Files in Python

A cartoon owl takes a document from a filing cabinet and drops it into a bin. Above, bold text reads “File Operations.”

What is pathlib?

When working with files in Python, you have two main choices: the older os module or the newer pathlib. The pathlib library was introduced to make file and folder handling much easier and more readable. Instead of juggling long strings with slashes, you work with Path objects that behave more like real files and directories. For example, Path("reports/summary.txt") is clearer than "reports\\summary.txt". In this tutorial we’ll mostly use pathlib because it’s beginner-friendly and works the same way across Windows, macOS, and Linux.

Renaming a File with Pathlib

The simplest way to rename a file is with Path.rename(). If you include a folder in the new name, it also moves the file.

Try this:

  • Create a file and rename it.
  • Try renaming a file that doesn’t exist to see FileNotFoundError.

Moving a File into a Folder

Renaming can also move files between folders.

Try this:

  • Create a folder and move a file into it.
  • Try moving to a folder that doesn’t exist and see what happens.

Deleting a File

To delete, use Path.unlink(). It removes the file permanently, not to the recycle bin.

Try this:

  • Delete a file, then try deleting it again to see FileNotFoundError.
  • Pass a folder path to trigger IsADirectoryError.

Common Exceptions in Basics

  • FileNotFoundError – the file doesn’t exist.
  • PermissionError – no rights, or file open elsewhere.
  • IsADirectoryError – you tried deleting a folder instead of a file.

Main Topic

Python Files and Directories

A cartoon owl with wide eyes stands in front of grey filing cabinets. One drawer is open, filled with folders, and the owl is holding a single document. The background is a warm orange tone, and the words “PYTHON FILES” appear in large bold text above the cabinets.

A light-hearted intro to handling files and directories in Python, featuring an owl mascot and fun “Python Files” imagery.

Other Tutorials in this Topic

A cartoon owl shrugs at a table with three letters: one in English, one in Arabic, and one in Chinese. Above it, bold text reads “CHARACTER ENCODINGS.”

Reading different Character Sets

This tutorial explains character encodings, why they exist, handling decoding errors, platform newline differences, and reading binary…

A cartoon owl with wide eyes stands against an orange background, holding and reading a letter. Above it, bold text reads “READING FILES IN PYTHON.”

Reading Files in Python

This tutorial teaches how to safely open and read files in Python, explore different reading methods, and…

A cheerful cartoon owl writes in an open notebook with a quill pen beside a filing cabinet drawer labelled “w, a, x.” Above, bold text reads “Writing Files in Python.”

Writing to Files in Python

Part 1 introduces writing to files in Python using context managers, explains file modes w, a, x,…

A cartoon owl sits at a desk holding two papers. One shows “Café 😊” clearly, the other displays scrambled symbols. Behind, a chalkboard reads “Character Encoding.”

Writing different Character Sets

This tutorial explains character encoding, why encodings differ, handling Unicode errors, appending safely, and using safer update…

A cartoon owl stands in front of a bookshelf, holding an old book while placing a new one on the shelf. Above, bold text reads “File Operations.”

More Python File Operations

This tutorial explores safer file operations, including overwriting with os.replace, moving across drives, creating backups, soft deletes,…

A cartoon owl stands at a crossroads holding a map. Signposts point to Linux (/home/), Windows (C:\Users), and Reports. Above, bold text reads “Navigating Directories in Python.”

Navigating Directories in Python

This tutorial explains navigating directories in Python with pathlib, covering creating, listing, deleting folders, handling exceptions, and…