Python 3 Pathlib Library Script to Remove or Delete Recursively a Directory or Folder

Python 3 Pathlib Library Script to Remove or Delete Recursively a Directory or Folder

Python 3 Pathlib Library Script to Remove or Delete Recursively a Directory or Folder

from pathlib import Path

def rmdir(directory):
    directory = Path(directory)
    for item in directory.iterdir():
        if item.is_dir():
            rmdir(item)
        else:
            item.unlink()
    directory.rmdir()

rmdir(Path("uploads"))
Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment