Python 3 OS Module Script to Remove all Directories or Folders and Subdirectories Using rmdir() Method Full Project For Beginners

Python 3 OS Module Script to Remove all Directories or Folders and Subdirectories Using rmdir() Method Full Project For Beginners

 

Welcome folks today in this blog post we will be removing all directories and subdirectories using os module in python. All the full source code of the application is given below.

 

 

Get Started

 

Removing Subdirectories

 

In order to get started you need to make an app.py file and copy paste the following code

 

app.py

 

import os

dir_path = 'uploads/temp'

try:
    os.rmdir(dir_path)
except OSError as e:
    print("Error: %s : %s" % (dir_path, e.strerror))

 

See also  Python 3 Script to Strip or Remove HTML Tags From Raw String Using Regular Expression Full Project For Beginners

 

Now as you can see inside my directory uploads we have a subdirectory temp as shown below

 

 

 

python app.py

 

 

So as you can see the subdirectory is deleted by using this script

 

Removing Whole Root Directory

 

app.py

 

import os

dir_path = 'uploads'

try:
    os.rmdir(dir_path)
except OSError as e:
    print("Error: %s : %s" % (dir_path, e.strerror))

Leave a Reply