Four Python Techniques to Safely Delete Folders on Windows
This article walks through four practical Python approaches—using the OS module, pathlib, shutil, and send2trash—to permanently or safely remove directories on Windows, explaining each method's behavior, required settings, and code examples for reliable folder deletion.
Multiple Methods to Delete a Folder (S Series)
The S series ("Small") records handy, sometimes quirky operations; this entry focuses on deleting a directory that contains data files on a Windows system.
Platform
Windows 10
Python 3.8
pywin32 227 (or pypiwin32)
send2trash 1.5.0
Purpose
Remove a folder that holds data files.
Deletion Methods
All methods are performed on Windows.
Method 1 : Right‑click the folder and choose Delete, or select the folder and press the Delete key. This moves the folder to the Recycle Bin; you can restore it by clicking Restore.
Method 2 : Use Python's os module.
import os
delete_dir = r'测试文件夹'
for r, d, f in os.walk(delete_dir, topdown=False):
for files in f:
os.remove(os.path.join(r, files)) # delete files
os.removedirs(r) # delete empty folderBy setting topdown=False , os.walk traverses from the deepest level outward, allowing non‑empty folders to be emptied before removal.
Method 3 : Use the pathlib module.
from pathlib import Path
delete_dir = Path(r'测试文件夹')
# delete all files
[i.unlink() for i in delete_dir.rglob('*') if i.is_file()]
# delete all empty sub‑folders
[i.rmdir() for i in delete_dir.rglob('*') if i.is_dir()]
# delete the target folder itself
delete_dir.rmdir()This separates file and folder deletion, performing two recursive passes before removing the top‑level directory.
Method 4 : Use shutil.rmtree.
from shutil import rmtree
delete_dir = r'测试文件夹'
rmtree(delete_dir)rmtree recursively deletes all files and sub‑folders. It can be combined with safety checks similar to Method 2.
Safe Delete to Recycle Bin : Install send2trash (or use pywin32 shell functions) to move items to the Recycle Bin instead of permanent removal.
import send2trash
delete_dir = r'测试文件夹'
send2trash.send2trash(delete_dir)Install the required packages with:
pip install send2trash
pip install pywin32 # or pip install pypiwin32Alternatively, use win32com.shell to invoke the shell's recycle‑bin API.
from win32com.shell import shell, shellcon
def recyclebin_empty(confirm=True, show_progress=True, sound=True):
flags = 0
if not confirm:
flags |= shellcon.SHERB_NOCONFIRMATION
if not show_progress:
flags |= shellcon.SHERB_NOPROGRESSUI
if not sound:
flags |= shellcon.SHERB_NOSOUND
shell.SHEmptyRecycleBin(None, None, flags)
recyclebin_empty(False, False, False)Summary
The article demonstrates several Python‑based techniques for deleting folders, ranging from manual Recycle Bin deletion to fully automated recursive removal with shutil.rmtree. While shutil.rmtree sometimes feels slower, tests showed that both shutil.rmtree and pathlib can achieve fast deletions depending on the environment.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
