OS.Removedirs() and shutil.Rmtree() are used to delete the folder


The OS. Removedirs () method is used to recursively delete a directory. Like rmdir (), if the subfolders are successfully deleted, removedirs () does not try their parent folder until an error is thrown (it is basically ignored because it generally means that your folder is not empty).


The syntax format of the removedirs() method is as follows:



path  — Directory path to remove

Return value

The method has no return value


The following example demonstrates the use of the removedirs() method:

import os,sys

import shutil


print "Before directory deletion: %s" % os.listdir(dstPath)

# Recursively delete directories and files

# shutil.rmtree('test/aa')

# The following two functions are used to delete empty directory files



print "After directory deletion: %s" % os.listdir(dstPath) 

Shutil module

shutil.copyfile( src, dst) # Copy from source src to dst. If the current dst already exists it will be overwritten

shutil.move( src, dst) # move the file or rename it

shutil.copymode( src, dst) #just copies its permissions, nothing else is copied

shutil.copystat( src, dst) #copy permissions, last access time, last modified time

shutil.copy( src, dst) #Copy a file to a file or a directory

shutil.copy2( src, dst) # copy the file last access time and modification time on top of the copy also copied over, similar to something like cp -p

shutil.copy2( src, dst) # if the two locations of the file system is the same then it is equivalent to rename operation, just change the name; if it is not in the same file system then it is to do the move operation

shutil.copytree( olddir, newdir, True/Flase) #Copy olddir to newdir, if the 3rd argument is True, then the symbolic link under the folder will be maintained when copying the directory, if the 3rd argument is False, then a physical copy will be generated in the copied directory instead of the symbolic Connections

shutil.rmtree( src ) # recursively delete a directory and all its contents

Read More: