Writing to Files in Python

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.”

Opening a File Safely

Always use a context manager (with ... as ...). It makes sure the file is closed properly, even if something goes wrong.

This is the cleanest and safest way to write to a file.

Try this:

  • Change the filename to somewhere you can’t write and trigger PermissionError.
  • Forget to close a file (by removing with) and see why it’s risky.

File Modes: w, a, and x

  • w (write): creates the file or clears it if it already exists.
  • a (append): adds text to the end without removing what’s already there.
  • x (exclusive create): makes a new file, but fails if the file already exists.

Try this:

  • Use x twice and see FileExistsError.
  • Write with w twice and notice the previous text disappears.

Writing Text

You can write with write, writelines, or even print.

Try this:

  • Save names without \n and see how they look in the file.
  • Add print statements and compare the difference.

Common Exceptions

  • FileExistsError: using x on an existing file.
  • PermissionError: you don’t have rights to write there.
  • IsADirectoryError: you passed 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 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 takes a document from a filing cabinet and drops it into a bin. Above, bold text reads “File Operations.”

Renaming and Deleting Files in Python

This tutorial covers renaming, moving, and deleting files in Python using pathlib, with examples, common exceptions, and…

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…