How to rename and replace a file in python with example

Sometimes, We need to rename a file with python code. It means renaming the name of the file and extension.

For example, If the file is emp.json, rename emp.json to test.json.

Python provides OS modules that contain rename method to rename the file.

Python renames a file

This short tutorial shows you how to rename with the os.rename() and os.replace() methods

  • use the os.rename() method os.rename() method is used to rename the given file path to the new given path name.
os.rename(source,destination)

source and destination contain a string of file paths

Returns None, if rename is successful, and also throws the error FileNotFoundError, if a file is not found FileExistsError, if the destination file already exists.

FileNotFoundError: [WinError 2] The system cannot find the file specified: ‘./emp1.kml’ -> ‘./emp.json’

  • Import the os module into the code
  • Create a File path using os.path.join() method called source path
  • Also, Create another path called the destination path, that renames the source to this file.
  • call os.rename() method
  • Returns None if it is successful
  • throws FileNotFoundError if the file is not found at the given path, and
  • FileExistsError, if the destination file already exists.
import os
sourcefile = os.path.join("./", "emp1.json")
destfile = os.path.join("./", "emp.json")
print(os.rename(sourcefile, destfile))

It successfully renamed a file

  • use os.replace() method

os.replace() method is used to replace the given file path to a new given path name. This does not throw FileExistsError if the destination file already exists.

os.replace(source,destination)

source and destination are string of file location paths

Returns None, if replace is successful, and also throws the error FileNotFoundError, if file is not found

FileNotFoundError: [WinError 2] The system cannot find the file specified: ‘./emp1.kml’ -> ‘./emp.json’

  • Import the os module into the code
  • Define a File path object using os.path.join() method called source path
  • Also, define another path called the destination path, which replaces the source of this file.
  • call os.replace() method
  • Returns None if it is successful
  • throws FileNotFoundError if the file is not found at the given path, and
import os
sourcefile = os.path.join("./", "emp1.json")
destfile = os.path.join("./", "emp.json")
print(os.replace(sourcefile, destfile))

What is the difference between the os rename and os replace methods in python

os rename used to rename the file to a new file os replace method is used to replace the file with the given file.

rename throws FileExistsError if the file already exists with the same name, so os.rename must be used to try and except blocks replace does not throw the FileExistsError, it just replaces the file

How to rename multiple files in a directory?

os.listdir(path) iterates the files in a directory. using for in loop to iterate each file call os.rename() rename the iterate file with destination file Here is an example for renaming the file which contains emp with newemp filename

import os
for filename in os.listdir("."):
print(filename)
os.rename(filename, filename.replace("emp", "newemp"))