How do i rename a folder in python os?



Description

Python method rename() renames the file or directory src to dst.If dst is a file or directory(already present), OSError will be raised.

Syntax

Following is the syntax for rename() method −

os.rename(src, dst)

Parameters

  • src − This is the actual name of the file or directory.

  • dst − This is the new name of the file or directory.

Return Value

This method does not return any value.

Example

The following example shows the usage of rename() method.

# !/usr/bin/python

import os, sys

# listing directories
print "The dir is: %s"%os.listdir(os.getcwd())

# renaming directory ''tutorialsdir"
os.rename("tutorialsdir","tutorialsdirectory")

print "Successfully renamed."

# listing directories after renaming "tutorialsdir"
print "the dir is: %s" %os.listdir(os.getcwd())\

When we run above program, it produces following result −

The dir is:
 [  'a1.txt','resume.doc','a3.py','tutorialsdir','amrood.admin' ]
Successfully renamed.
The dir is:
 [  'a1.txt','resume.doc','a3.py','tutorialsdirectory','amrood.admin' ]

python_files_io.htm

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.”

How do i rename a folder in python os?

  • 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()

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.

    os.rename() method in Python is used to rename a file or directory.
    This method renames a source file/ directory to specified destination file/directory.

    Syntax: os.rename(source, destination, *, src_dir_fd = None, dst_dir_fd = None)

    Parameters:
    source: A path-like object representing the file system path. This is the source file path which is to renamed.
    destination: A path-like object representing the file system path.
    src_dir_fd (optional): A file descriptor referring to a directory.
    dst_dir_fd (optional): A file descriptor referring to a directory.

    Return Type: This method does not return any value.

    Code #1: Use of os.rename() method

    import os

    source = 'GeeksforGeeks/file.txt'

    dest = 'GeekforGeeks/newfile.txt'

    os.rename(source, dest)

    print("Source path renamed to destination path successfully.")

    Output:

    Source path renamed to destination path successfully.
    

    Code #2: Handling possible errors

    import os

    source = './GeeksforGeeks/file.txt'

    dest = './GeeksforGeeks/dir'

    try :

        os.rename(source, dest)

        print("Source path renamed to destination path successfully.")

    except IsADirectoryError:

        print("Source is a file but destination is a directory.")

    except NotADirectoryError:

        print("Source is a directory but destination is a file.")

    except PermissionError:

        print("Operation not permitted.")

    except OSError as error:

        print(error)

    Output:

    Source is a file but destination is a directory.
    

    Reference: https://docs.python.org/3/library/os.html#os.rename


    How do I rename a folder in Python?

    rename() method in Python is used to rename a file or directory. This method renames a source file/ directory to specified destination file/directory.

    How do I rename a file in Python os?

    Steps to Rename File in Python.
    Find the path of a file to rename. To rename a file, we need its path. The path is the location of the file on the disk. ... .
    Decide a new name. Save an old name and a new name in two separate variables. old_name = 'details.txt' ... .
    Use rename() method of an OS module. Use the os..

    What is os rename in Python?

    Python method rename() renames the file or directory src to dst. If dst is a file or directory(already present), OSError will be raised.

    How do I rename a folder in Pycharm?

    Right-click the root folder of your project and select Refactor | Rename from the context menu or press Shift+F6 . In the dialog that opens, choose the rename strategy. If the project name is the same as the name of its root folder, select Rename directory.