• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Using Python to Remove Non-Empty Directory

Using Python to Remove Non-Empty Directory

June 20, 2022 Leave a Comment

To remove a non-empty directory in Python, the easiest way is to use the shutil module rmtree() function.

import shutil

shutil.rmtree(path)

When working with files and folders in Python, the ability to easily create or delete files and folders can be useful.

One such situation is if you want to remove a non-empty directory.

The shutil module rmtree() function allows you to delete an entire directory tree given a path.

With rmtree(), you can delete any directory and all of its contents. rmtree() takes three parameters.

shutil.rmtree(path, ignore_errors=False, onerror=None)

The first parameter is a string representing the path of the directory you want to remove. The second parameter, ‘ignore_errors’ allows you to ignore any errors that may occur when attempting to delete a directory and its contents. For the third parameter, ‘onerror’, you can pass a function which will handle any errors that may occur during the attempted removal of the directory.

Below is a simple example showing you how to delete a directory with rmtree() in Python.

import shutil

shutil.rmtree(path)

Hopefully this article has been useful for you to learn how to remove non-empty directories using Python.

Other Articles You'll Also Like:

  • 1.  Scroll Down Using Selenium in Python
  • 2.  Using Python to Count Even Numbers in List
  • 3.  Find Index of Maximum in List Using Python
  • 4.  pandas mode – Find Mode of Series or Columns in DataFrame
  • 5.  Using pandas resample() to Resample Time Series Data
  • 6.  How to Iterate over Everything in Word Document using python-docx
  • 7.  Find Least Common Multiple of Numbers Using Python
  • 8.  Keep Every Nth Element in List in Python
  • 9.  Shift Values in a List Using Python
  • 10.  Count Number of Keys in Dictionary in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy