YouTip LogoYouTip

Python3 Os Rename

# Python3.x Python3 os.rename() Method [![Image 3: Python3 OS File/Directory Methods](#) Python3 OS File/Directory Methods](#) * * * ### Overview The os.rename() method is used to rename a file or directory from src to dst. If dst is an existing directory, an OSError will be raised. ### Syntax The syntax for the **rename()** method is as follows: os.rename(src, dst) ### Parameters * **src** -- The directory name to be modified. * **dst** -- The new directory name. ### Return Value This method does not return a value. ### Example The following example demonstrates the use of the rename() method: #!/usr/bin/python3import os, sys # List the directoryprint ("The directory is: %s"%os.listdir(os.getcwd()))# Rename os.rename("test","test2")print ("Rename successful.")# List the directory after renamingprint ("The directory is: %s" %os.listdir(os.getcwd())) The output of the above program is: The directory is:[ 'a1.txt','resume.doc','a3.py','test' ]Rename successful.[ 'a1.txt','resume.doc','a3.py','test2' ] [![Image 4: Python3 OS File/Directory Methods](#) Python3 OS File/Directory Methods](#)
← Python3 Os RmdirPython3 Os Rename β†’