How to Rename or Move a File/Folder/Directory in Python

This tutorial will explain how to rename or move a file or directory in the Python programming language and provide some simple examples you can follow.

Looking to delete a file? Check out our tutorial on file/folder deletion in Python here.

To Avoid Errors when Renaming, First Check if a File or Folder Exists

To avoid errors when renaming or moving a file or folder, you should first check whether the path exists. The file which is to be moved or renamed must exist, and you may also want to check whether the destination exists so that it isn’t overwritten (unless you intend to overwrite the file at the destination).

Check Permissions of the Source and Destination

You should also check the permissions of both the source and destination paths. You will require write access to both paths to rename or move a file or folder.

Renaming vs Moving

This article covers both renaming a file and moving a file – because they are effectively the same thing.

In both instances the path to the source (the file or directory to be renamed) and the destination (the new, renamed path) are supplied. If a relative or absolute path are supplied as the destination, the rename process will also move the file to that new location.

Rename or Move a File

The easiest way to rename a file is using the rename() function from the built-in Python os library. The os library communicates with the host operating system and performs basic file operations:

# Import the os library
import os

# Rename a file

# Move a file

Note that both the source and destination paths must refer to either a file or directory – not both.

The same rename() function can be used with directories:

Rename or Move a Folder/Directory

# Import the os library
import os

# Rename a directory

# Move a directory

Moving Files and Folders with the shutil Library

The built-in shutil library provides higher level file handling functions for Python, and can be used to rename and move files and directories.

# Import the shutil library
import shutil 

# Move a file
shutil.move("path/to/file.txt", "new/path/to/file.txt")

Using Path Objects

Path objects created using the pathlib library represent files and directories in an object-oriented manner in your Python code.

Path objects contain instances for moving and renaming the files or directories at a specified path:

# Import the pathlib library
import pathlib

# Create a path object for a file or folder on disk
myFile = pathlib.Path('path/to/file.txt')

# Rename the file on disk

The file will be renamed and the path object will be updated to represent the file at its new location.

Be Careful!

The above functions do not prompt for confirmation before moving and renaming files!

Be careful that you specify the correct paths so that you don’t accidentally overwrite something important or attempt to move a file somewhere out of reach!

Photo of author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment