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.

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

The dir after removal of path :

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

os.removedirs(path)

Description

The method removedirs() removes dirs recursively. If the leaf directory is succesfully removed, removedirs tries

to successively remove every parent directory displayed in path.

Syntax

Following is the syntax for removedirs() method:

os.removedirs(path)

Parameters

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

Return Value

This method does not return any value.

Example

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

# !/usr/bin/python

import os, sys

# listing directories

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

# removing

os.removedirs("/tutorialsdir")

# listing directories after removing directory

print "The dir after removal is:" %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' ]

The dir after removal is:

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

os.rename(src, dst)

Description

The method rename() renames the file or directory src to dst.If dst is a file or directory(already

present),OSError will be raised.

TUTORIALS POINT

Simply Easy Learning

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

Saved successfully!

Ooh no, something went wrong!