YouTip LogoYouTip

Os Rename

# Python2.x Python os.rename() Method [![Image 3: Python File Methods](#) Python OS File/Directory Methods](#) * * * ### Overview The os.rename() method is used to rename files or directories from src to dst. If dst is an existing directory, an OSError will be raised. ### Syntax The syntax format of the **rename()** method is as follows: os.rename(src, dst) ### Parameters * **src** -- The name of the file or directory to be modified * **dst** -- The modified name of the file or directory ### Return Value This method does not return a value. ### Example The following example demonstrates the use of the rename() method: #!/usr/bin/python# -*- coding: UTF-8 -*-import os, sys # List directory print "directory is: %s"%os.listdir(os.getcwd())# Rename os.rename("test","test2")print "Rename successful.# List directory after renaming print "directory is: %s" %os.listdir(os.getcwd()) The output of executing the above
← Linux Comm AwkLinux Comm Scp β†’