Automate Deleting Massive Windows Files and Bypass Long Path Limits with Python
This article explains how to use Python's built‑in functions and the os.walk method to automatically locate and delete files or directories belonging to specific subsystems, while also addressing Windows' long‑path limitation by prefixing paths with the special "\\?\" syntax.
0x01 Article Background
Recently the storage of a business system is near its limit. The system contains many subsystems (A1, A2, …) whose intermediate files are stored in a common parent directory, with filenames prefixed by the subsystem name (e.g., A1xxxxxx, A2xxxxx). To free space, we need to delete historical files of selected subsystems automatically using Python.
0x02 Using Python to Delete Files
Python provides three built‑in functions for deletion: os.remove() – delete a file os.rmdir() – delete an empty directory shutil.rmtree() – delete a directory and all its contents
The task can be broken into four sub‑problems: (1) delete a file, (2) identify files/folders belonging to a subsystem, (3) determine whether a path is a file or directory, and (4) locate all target files.
For (1) we simply call the functions above. For (2) we match the subsystem keyword in the filename. For (3) we use os.path.isdir() and os.path.isfile(). For (4) we traverse the directory tree with os.walk():
import os
path = "C:\\A\\"
for root, dirs, files in os.walk(path):
print(root)
print(dirs)
print(files)Combining these steps yields the following implementation:
import os
import shutil
path = "C:\\A\\"
keyword = "A1"
for root, dirs, files in os.walk(path):
for dir in dirs:
if keyword in dir:
rmpath = os.path.join(root, dir)
print("Deleting folder: %s" % rmpath)
shutil.rmtree(rmpath)
for file in files:
if keyword in file:
rmpath = os.path.join(root, file)
print("Deleting file: %s" % rmpath)
os.remove(rmpath)0x03 Windows Long‑Path Limitation
Windows API supports paths up to 32 767 bytes, but the user‑mode limit is 256 characters. Exceeding this limit causes errors such as WindowsError: The system cannot find the path specified. The limitation can be bypassed by prefixing the absolute path with the special string \\?\.
0x04 Modified Python Script for Long Paths
Adding a single line that prepends \\?\ to the absolute path enables deletion of long‑path files:
# Get absolute path and add the \?\ prefix to lift the Windows length restriction
path = '\\?\\' + os.path.abspath(path)The final script combines the traversal logic with the long‑path fix, providing a compact tool that reliably removes unwanted files and directories, even when their paths exceed the default Windows limit.
0x05 Summary
Decompose a complex deletion task into smaller, manageable problems.
Consult official documentation; often a single line of code solves the core issue.
Leverage Python’s powerful standard library for file‑system automation.
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.
