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]. This is the syntax for os.rename[] method 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: 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.”Syntax
os.rename[src, dst]
Parameters
import os
os.rename['guru99.txt','career.guru99.txt']
- 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
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: //docs.python.org/3/library/os.html#os.rename