How to Write to a File in Python

If you want to save text from your Python program into a file, use the built-in open() function.

This page shows the simplest way to:

  • write text to a file
  • create a file if it does not exist
  • choose the right file mode
  • avoid overwriting a file by mistake

Quick answer

with open("notes.txt", "w") as file:
    file.write("Hello, world!\n")

This creates notes.txt if it does not exist.

The "w" mode overwrites the file if it already exists.

What this page helps you do

  • Write text to a file in Python
  • Create a new file if it does not exist
  • Understand the difference between write modes
  • Avoid overwriting a file by mistake

Basic way to write to a file

The most common pattern is:

with open("notes.txt", "w") as file:
    file.write("Hello, world!\n")

How it works

  • open() opens the file
  • "w" means write mode
  • write() writes text into the file
  • with closes the file automatically when you are done

This is the safest beginner-friendly way to write to a file.

Expected result

After running the code, the file notes.txt will contain:

Hello, world!

If you want to understand open() in more detail, see Python open() function explained.

What the key parts mean

Look at this line again:

with open("notes.txt", "w") as file:

Here is what each part means:

  • "notes.txt" is the file name
  • "w" means write mode
  • file is the variable that refers to the opened file

Then this line writes the text:

file.write("Hello, world!\n")

Key points:

  • write() writes a string to the file
  • \n adds a new line

Without \n, the next text you write will continue on the same line.

Common file modes for writing

When writing to files, these modes are the most useful:

"w" mode

with open("notes.txt", "w") as file:
    file.write("First line\n")
  • Writes from the start of the file
  • Replaces old content if the file already exists
  • Creates the file if it does not exist

Use this when you want to replace everything in the file.

"a" mode

with open("notes.txt", "a") as file:
    file.write("Another line\n")
  • Adds new content to the end of the file
  • Keeps existing content
  • Creates the file if it does not exist

Use this when you want to keep what is already there. For more on this, see how to append to a file in Python.

"x" mode

with open("notes.txt", "x") as file:
    file.write("New file created\n")
  • Creates a new file
  • Fails if the file already exists

Use this when you only want to create a brand-new file.

Choose the mode based on whether you want to replace old content or keep it.

Writing multiple lines

You can call write() more than once:

with open("notes.txt", "w") as file:
    file.write("Line 1\n")
    file.write("Line 2\n")
    file.write("Line 3\n")

The file will contain:

Line 1
Line 2
Line 3

You can also write one longer string:

with open("notes.txt", "w") as file:
    file.write("Line 1\nLine 2\nLine 3\n")

Both approaches work.

Important: write() does not add line breaks for you. If you want each line on a new line, include \n yourself.

Writing numbers and other non-string values

The write() method only accepts strings.

This will cause an error:

with open("score.txt", "w") as file:
    file.write(100)

You will get a TypeError because 100 is an integer, not a string.

Correct way

Convert the value with str() first:

with open("score.txt", "w") as file:
    file.write(str(100))

You can do the same for floats and other values:

temperature = 23.5

with open("temp.txt", "w") as file:
    file.write(str(temperature))

When to use append instead

Use "a" mode if you want to keep existing content.

This is useful for:

  • logs
  • notes
  • saving repeated results
  • adding new entries over time

Example:

with open("log.txt", "a") as file:
    file.write("Program started\n")

If log.txt already has text in it, the new line is added at the end.

Do not use "w" if you need to preserve what is already in the file. If that is your goal, read how to append to a file in Python.

Common problems beginners hit

Here are some common mistakes when writing to files.

1. File content disappears

Cause:

  • You used "w" mode, which overwrites the file

Example:

with open("notes.txt", "w") as file:
    file.write("New text\n")

If notes.txt already had content, it is replaced.

Fix:

  • Use "a" mode if you want to keep old content

2. Text does not appear on a new line

Cause:

  • You forgot to add \n

Example:

with open("notes.txt", "a") as file:
    file.write("First line")
    file.write("Second line")

Result:

First lineSecond line

Fix:

with open("notes.txt", "a") as file:
    file.write("First line\n")
    file.write("Second line\n")

3. TypeError when writing a number

Cause:

  • write() expects a string

Fix:

age = 25

with open("age.txt", "w") as file:
    file.write(str(age))

4. File path errors

Cause:

  • The file location is wrong
  • You are using a folder name that does not exist

Example:

with open("missing_folder/notes.txt", "w") as file:
    file.write("Hello\n")

If the folder does not exist, writing will fail.

If you are seeing a missing file or bad path problem, see FileNotFoundError: Errno 2 No such file or directory fix.

5. Permission errors

Cause:

  • Python does not have permission to write to that location

This can happen with protected folders or system locations.

If that happens, see PermissionError: Errno 13 Permission denied fix.

Simple debugging checks

If writing to a file is not working, check these first:

  • Print the file name or full path you are using
  • Check whether you opened the file in "w" or "a" mode
  • Make sure the value passed to write() is a string
  • Check folder permissions if writing fails

Useful debug commands:

print(type(value))
print(file_path)

import os
print(os.getcwd())

import os
print(os.path.exists(file_path))

What these help you check

  • print(type(value)) shows whether the value is a string
  • print(file_path) shows the exact file path being used
  • os.getcwd() shows the current working folder
  • os.path.exists(file_path) checks whether the path exists

If you need help with file locations, read how to check if a file exists in Python.

FAQ

Does open() create the file if it does not exist?

Yes. In "w" and "a" mode, Python creates the file if it does not already exist.

What is the difference between "w" and "a" mode?

"w" overwrites the file. "a" adds new content to the end.

Why does write() not put text on a new line automatically?

write() writes exactly what you give it. Add \n if you want a line break.

Can I write numbers to a file?

Yes, but convert them first with str(). The write() method expects a string.

Do I need to close the file manually?

Not if you use a with statement. Python closes it for you automatically.

See also