---
description: In Python, rename() method is used to rename a file or directory. It takes two arguments. os.rename(src, dst)
title: Python Rename File and Directory using os.rename()
image: https://www.guru99.com/images/python-rename-file.png
---

 

[Skip to content](#main) 

**⚡ Smart Summary**

Python renames files and directories with os.rename(). Here you will learn os.rename(), os.renames(), shutil.move(), and pathlib Path.rename(), plus how to rename a directory and batch-rename multiple files with clear examples.

* ✏️ **os.rename():** os.rename(src, dst) renames a file or directory; the source must exist.
* 🗂️ **os.renames():** os.renames() also creates missing destination folders automatically.
* 🚚 **shutil.move():** shutil.move() renames and can move files across file systems.
* 🐍 **pathlib:** Path.rename() renames files the object-oriented way (Python 3.4+).
* 🤖 **AI help:** AI tools like Copilot generate rename and batch-rename code instantly.

[ Read More ](javascript:void%280%29;) 

![Python Rename File os.rename\(\)](https://www.guru99.com/images/rename-file-in-python-os-rename.png)

## Python Rename File

**Python rename() file** is a method used to rename a file or a directory in Python programming. The Python rename() file method can be declared by passing two arguments named src (Source) and dst (Destination).

### Syntax

This is the syntax for os.rename() method

os.rename(src, dst)

### Parameters

**src:** Source is the name of the file or directory. It should must already exist.

**dst:** Destination is the new name of the file or directory you want to change.

**Example:**

import os  
os.rename('guru99.txt','career.guru99.txt')

Let’s look at example in detail

You can rename the original file, we have changed the file name from “Guru99.txt” to “Career.guru99.txt.”

[](https://www.guru99.com/images/Pythonnew/Python18.9.png)

* To rename “guru99.txt” file, we going to use “rename function” in the OS module
* So when the code is executed, you can observe that a new file “career.guru99.txt” is created on the right side of the panel, which we renamed for our original file.

Here is the complete code

import os
import shutil
from os import path

def main():
	# make a duplicate of an existing file
    if path.exists("guru99.txt"):
	# get the path to the file in the current directory
        src = path.realpath("guru99.txt");
		
	# rename the original file
        os.rename('guru99.txt','career.guru99.txt') 
		
if __name__ == "__main__":
    main()

## How to Rename a Directory in Python

The os.rename() method also renames directories, not just files. Pass the current folder name as the source and the new name as the destination.

import os
os.rename("old_directory", "new_directory")

If the source directory does not exist, Python raises a FileNotFoundError, so it is good practice to check the path first.

### RELATED ARTICLES

* [How to Install Python on Windows \[Pycharm IDE\] ](https://www.guru99.com/how-to-install-python.html "How to Install Python on Windows [Pycharm IDE]")
* [Python Main Function & Method: Understand def Main() ](https://www.guru99.com/learn-python-main-function-with-examples-understand-main.html "Python Main Function & Method: Understand def Main()")
* [Difference Between Python and C++ ](https://www.guru99.com/python-vs-c-plus-plus.html "Difference Between Python and C++")
* [Python readline() Method with Examples ](https://www.guru99.com/python-file-readline.html "Python readline() Method with Examples")

## Rename a File Using os.renames()

The os.renames() method renames a file and automatically creates any missing intermediate directories in the destination path. It also removes empty directories left behind in the source path.

import os
os.renames("guru99.txt", "archive/2024/career.txt")

Here the folders archive and 2024 are created automatically if they do not already exist.

## Rename a File Using shutil.move()

The shutil.move() function moves a file to a new location, which effectively renames it when the destination is the same directory.

import shutil
shutil.move("guru99.txt", "career.guru99.txt")

Unlike os.rename(), shutil.move() can also move a file across different file systems or drives.

## Rename a File Using the pathlib Module

In Python 3.4 and later, the pathlib module offers an object-oriented Path.rename() method for renaming files.

from pathlib import Path

file = Path("guru99.txt")
file.rename("career.guru99.txt")

The rename() method returns a new Path object that points to the renamed file.

## How to Rename Multiple Files in Python

To rename many files at once, loop over the files in a directory with os.listdir() and call os.rename() for each one. The example below adds a numbered prefix to every file.

import os

folder = "myfiles"
for count, filename in enumerate(os.listdir(folder)):
    new_name = f"file_{count}.txt"
    os.rename(os.path.join(folder, filename), os.path.join(folder, new_name))

Using os.path.join() keeps the code working correctly on any operating system.

## FAQs

✏️ How do you rename a file in Python?

Import os and call os.rename(src, dst), passing the current file name and the new name. The source file must already exist, or Python raises FileNotFoundError.

🗂️ What is the difference between os.rename() and os.renames()?

os.rename() renames a single file or directory, while os.renames() also creates any missing intermediate directories and removes empty source folders after the move.

🚚 Can you rename a file with shutil?

Yes. shutil.move(src, dst) renames a file when the destination is the same folder, and can also move files across different file systems, unlike os.rename().

📁 How do you rename a directory in Python?

Use os.rename(‘old\_dir’, ‘new\_dir’). The same os.rename() function works for both files and directories, as long as the source directory exists.

🔢 How do you rename multiple files at once?

Loop over os.listdir(folder) and call os.rename() for each file. Use os.path.join() to build full paths so the code works on any operating system.

🤖 How can AI tools like GitHub Copilot help rename files?

AI assistants like GitHub Copilot autocomplete os.rename() and pathlib calls, and generate batch-rename loops from a comment describing the naming pattern you want.

🧠 Can AI automate bulk file renaming in Python?

Yes. AI-powered tools can automate writing batch-rename scripts, apply patterns like numbering or date prefixes, and add error handling for missing or duplicate names.

⚠️ What happens if the destination file already exists?

On Windows, os.rename() raises FileExistsError if the destination exists. On Unix, it silently overwrites. Check with os.path.exists() first to avoid losing data.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/python-rename-file.png","url":"https://www.guru99.com/images/python-rename-file.png","width":"862","height":"366","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/python-rename-file.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/python","name":"Python"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/python-rename-file.html","name":"Python Rename File and Directory using os.rename()"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/python-rename-file.html#webpage","url":"https://www.guru99.com/python-rename-file.html","name":"Python Rename File and Directory using os.rename()","dateModified":"2026-07-10T19:49:21+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/python-rename-file.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/python-rename-file.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/anna","name":"Anna Blake","description":"I'm Anna Blake, specializing in Python tutorials, offering clear and concise lessons to help you master Python programming efficiently.","url":"https://www.guru99.com/author/anna","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/anna-blake-author.png","url":"https://www.guru99.com/images/anna-blake-author.png","caption":"Anna Blake","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"Python","headline":"Python Rename File and Directory using os.rename()","description":"In Python, rename() method is used to rename a file or directory. It takes two arguments. os.rename(src, dst)","keywords":"python","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/anna","name":"Anna Blake"},"dateModified":"2026-07-10T19:49:21+05:30","image":{"@id":"https://www.guru99.com/images/python-rename-file.png"},"copyrightYear":"2026","name":"Python Rename File and Directory using os.rename()","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"How do you rename a file in Python?","acceptedAnswer":{"@type":"Answer","text":"Import os and call os.rename(src, dst), passing the current file name and the new name. The source file must already exist, or Python raises FileNotFoundError."}},{"@type":"Question","name":"What is the difference between os.rename() and os.renames()?","acceptedAnswer":{"@type":"Answer","text":"os.rename() renames a single file or directory, while os.renames() also creates any missing intermediate directories and removes empty source folders after the move."}},{"@type":"Question","name":"Can you rename a file with shutil?","acceptedAnswer":{"@type":"Answer","text":"Yes. shutil.move(src, dst) renames a file when the destination is the same folder, and can also move files across different file systems, unlike os.rename()."}},{"@type":"Question","name":"How do you rename a directory in Python?","acceptedAnswer":{"@type":"Answer","text":"Use os.rename('old_dir', 'new_dir'). The same os.rename() function works for both files and directories, as long as the source directory exists."}},{"@type":"Question","name":"How do you rename multiple files at once?","acceptedAnswer":{"@type":"Answer","text":"Loop over os.listdir(folder) and call os.rename() for each file. Use os.path.join() to build full paths so the code works on any operating system."}},{"@type":"Question","name":"How can AI tools like GitHub Copilot help rename files?","acceptedAnswer":{"@type":"Answer","text":"AI assistants like GitHub Copilot autocomplete os.rename() and pathlib calls, and generate batch-rename loops from a comment describing the naming pattern you want."}},{"@type":"Question","name":"Can AI automate bulk file renaming in Python?","acceptedAnswer":{"@type":"Answer","text":"Yes. AI-powered tools can automate writing batch-rename scripts, apply patterns like numbering or date prefixes, and add error handling for missing or duplicate names."}},{"@type":"Question","name":"What happens if the destination file already exists?","acceptedAnswer":{"@type":"Answer","text":"On Windows, os.rename() raises FileExistsError if the destination exists. On Unix, it silently overwrites. Check with os.path.exists() first to avoid losing data."}}]}],"@id":"https://www.guru99.com/python-rename-file.html#schema-1140455","isPartOf":{"@id":"https://www.guru99.com/python-rename-file.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/python-rename-file.html#webpage"}}]}
```
