28.10.2021 Views

Python Tutorial ( PDFDrive )

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Let us compile and run the above program, this will produce the 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' ]

os.renames(old, new)

Description

The method renames() is recursive directory or file renaming function. It does the same functioning as

os.rename(), but it also moves a file to a directory, or a whole tree of directories, that do not exist.

Syntax

Following is the syntax for renames() method:

os.renames(old, new)

Parameters

• old -- This is the actual name of the file or directory to be renamed.

TUTORIALS POINT

Simply Easy Learning

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!