To delete a non-empty folder in Python, you can use the shutil module. This module provides several functions for deleting and managing directories.

Here’s an example of how you can use the shutil.rmtree function to delete a non-empty folder:

				
					import shutil

folder_path = '/path/to/folder'

# Delete the folder and all its contents
shutil.rmtree(folder_path)

				
			

Note that shutil.rmtree will delete the specified folder and all its contents, including any subdirectories and files. So be careful when using this function, as it permanently removes its target files and directories.

You can also use the os module to delete a non-empty folder. Here’s an example of how you can use the os.rmdir and os.removers functions to delete a non-empty folder:

				
					import os

folder_path = '/path/to/folder'

# Delete all the files and subdirectories in the folder
for root, dirs, files in os.walk(folder_path, topdown=False):
    for name in files:
        os.remove(os.path.join(root, name))
    for name in dirs:
        os.rmdir(os.path.join(root, name))

# Delete the folder
os.rmdir(folder_path)

# Delete any empty parent folders (if the deleted folder was a subdirectory)
os.removedirs(folder_path)

				
			

This code first recursively deletes all the files and subdirectories in the specified folder, and then deletes the folder itself. Finally, it removes any empty parent folders if the deleted folder was a subdirectory.

Alternative Approaches

1. pathlib Module

To delete a non-empty folder using the pathlib module in Python, you can use the Path.rmtree method. This method removes the specified directory and all its contents, including any subdirectories and files.

Here’s an example of how you can use Path.rmtree to delete a non-empty folder:

				
					from pathlib import Path

folder_path = '/path/to/folder'

# Delete the folder and all its contents
folder = Path(folder_path)
folder.rmtree()

				
			

Note that Path.rmtree permanently removes the files and directories it targets, so be careful when using it.

You can also use the Path.unlink method to delete individual files within a folder, or the Path.rmdir method to delete empty subdirectories. For example:

				
					from pathlib import Path

folder_path = '/path/to/folder'
file_path = '/path/to/folder/file.txt'
subdir_path = '/path/to/folder/subdir'

# Delete a file
file = Path(file_path)
file.unlink()

# Delete an empty subdirectory
subdir = Path(subdir_path)
subdir.rmdir()

				
			

2. The ‘os.walk’ Function

To delete a non-empty folder using the os.walk function in Python, you can use the following steps:

  1. Iter over the folder’s contents using os.walk, which returns a tuple containing the root directory, a list of subdirectories, and a list of files.
  2. For each file in the list of files, use the os.remove function to delete the file.
  3. For each subdirectory in the list of subdirectories, use the os.rmdir function to delete the subdirectory if it is empty, or recursively delete its contents using the same process.
  4. Once all the files and subdirectories have been deleted, use the os.rmdir function to delete the root directory.

Here’s an example of how you can use os.walk to delete a non-empty folder:

				
					import os

folder_path = '/path/to/folder'

# Delete all the files and subdirectories in the folder
for root, dirs, files in os.walk(folder_path, topdown=False):
    for name in files:
        os.remove(os.path.join(root, name))
    for name in dirs:
        os.rmdir(os.path.join(root, name))

# Delete the folder
os.rmdir(folder_path)

				
			

Note that this approach relies on the os.walk function, which recursively traverses the directory tree and returns the contents of each directory in turn. There may be more efficient ways to delete a large folder with many files and subdirectories.

Deleting a non-empty directory in Python is more complex than you think. But once you understand the proper steps, it’s pretty easy. Just remember to delete all files and subdirectories inside the directory, and then delete the virtual directory itself. With these tips in mind, you can successfully delete any non-empty folder using Python.


Thanks for reading. Happy coding!