How to Rename a File in Python

If you want to rename a file in Python, the simplest built-in option is os.rename().

This page shows you how to:

  • Rename one existing file
  • Use Python’s built-in os.rename() function
  • Understand what the old path and new path mean
  • Avoid common path and file existence mistakes

Quick answer

import os

old_name = "old_file.txt"
new_name = "new_file.txt"

os.rename(old_name, new_name)
print("File renamed")

This works when the file exists and both paths are correct.

If the file is in a different folder, use full paths instead of just the file names.

What this page helps you do

  • Rename one existing file
  • Use Python's built-in os.rename() function
  • Understand what the old path and new path mean
  • Avoid common path and file existence mistakes

Basic way to rename a file

To rename a file with os.rename():

  1. Import the os module
  2. Pass the current file name as the first argument
  3. Pass the new file name as the second argument
import os

os.rename("old_file.txt", "new_file.txt")

In this example:

  • "old_file.txt" is the current name of the file
  • "new_file.txt" is the new name you want to give it

Run this code from the folder where the file exists, or use full paths.

If you are not sure whether the file is really there, see how to check if a file exists in Python.

Example with file names in the current folder

This is the easiest example for beginners.

It works when:

  • Your Python script is in the same folder as the file
  • The file is already named old_file.txt
import os

old_name = "old_file.txt"
new_name = "new_file.txt"

os.rename(old_name, new_name)

print("Renamed successfully")

Expected result

After the code runs:

  • old_file.txt no longer appears in the folder
  • new_file.txt appears in the same folder
  • The file contents stay the same

Example with full file paths

Use full paths when the file is in another folder.

This helps avoid confusion about the current working directory.

import os

old_name = r"C:\Users\YourName\Documents\old_file.txt"
new_name = r"C:\Users\YourName\Documents\new_file.txt"

os.rename(old_name, new_name)

print("Renamed successfully")

Why this is useful:

  • You tell Python exactly where the file is
  • You do not depend on where the script is running
  • Path errors are less common

If file paths are confusing, read working with file paths in Python.

Check that the file exists before renaming

A common error is trying to rename a file that is not there.

You can check first with os.path.exists():

import os

old_name = "old_file.txt"
new_name = "new_file.txt"

if os.path.exists(old_name):
    os.rename(old_name, new_name)
    print("File renamed")
else:
    print("The file does not exist")

This is helpful because:

  • It can prevent a FileNotFoundError
  • It gives you a clearer message
  • It makes your code safer

If you get this error anyway, see how to fix FileNotFoundError: No such file or directory.

Common problems when renaming files

Here are the most common reasons file renaming fails:

  • The old file name is wrong
  • The file is in a different folder
  • The new file name already exists
  • You do not have permission to change the file
  • You passed a folder path instead of a file path

Common causes

  • Misspelled old file name
  • Wrong folder or working directory
  • Using a path to a directory instead of a file
  • Trying to rename a file without permission
  • Using relative paths without knowing where the script is running

Debugging commands

These quick checks can help:

import os
print(os.getcwd())

Shows the current working directory.

import os
print(os.path.exists('old_file.txt'))

Checks whether the path exists.

import os
print(os.listdir())

Shows files and folders in the current directory.

import os
print(os.path.isfile('old_file.txt'))

Checks whether the path is a file.

If Python says permission is denied, read how to fix PermissionError: Errno 13 permission denied.

When to use os.rename() vs pathlib

For a beginner, os.rename() is a good place to start because it is simple and direct.

  • Use os.rename() for the simplest example
  • pathlib is also a valid option
  • pathlib often reads more clearly
  • This page stays focused on the basic built-in solution

If you want to understand os.rename() in more detail, see the os.rename() function explained.

Expected result

When the rename works:

  • The old file name no longer appears
  • The new file name appears in the same folder unless you changed the path
  • The file contents stay the same

Also, if the new path points to another folder, the file may be moved and renamed in one step.

FAQ

Does renaming a file change its contents?

No. It changes the file name or path, not the data inside the file.

Can I move a file by renaming it?

Yes. If you give a new path in another folder, Python can move and rename it in one step in many cases.

What if Python says the file does not exist?

Check the file name, extension, and folder path.

Also print os.getcwd() to see where your script is running.

What function renames a file in Python?

A common built-in option is os.rename().

See also