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.

• new -- This is the new name of the file or directory.It can even include a file to a directory, or a whole tree of

directories, that do not exist.

Return Value

This method does not return any value.

Example

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

# !/usr/bin/python

import os, sys

print "Current directory is: %s" %os.getcwd()

# listing directories

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

# renaming file "aa1.txt"

os.renames("aa1.txt","newdir/aanew.txt")

print "Successfully renamed."

# listing directories after renaming and moving "aa1.txt"

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

Let us compile and run the above program, this will produce the following result:

Current directory is: /tmp

The dir is:

[ 'a1.txt','resume.doc','a3.py','aa1.txt','Administrator','amrood.admin' ]

Successfully renamed.

The dir is:

[ 'a1.txt','resume.doc','a3.py','Administrator','amrood.admin' ]

The file aa1.txt is not visible here, as it is been moved to newdir and renamed as aanew.txt. The

directory newdir and its contents are shown below:

[ 'aanew.txt' ]

os.rmdir(path)

Description

The method rmdir() removes the directory path. It works only when the directory is empty, else OSErroris raised.

Syntax

Following is the syntax for rmdir() method:

os.rmdir(path)

Parameters

• path -- This is the path of the directory, which needs to be removed.

TUTORIALS POINT

Simply Easy Learning

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

Saved successfully!

Ooh no, something went wrong!