Created
April 28, 2024 18:17
-
-
Save pbeens/262347215e87b9c03e8bf7a79b6c5132 to your computer and use it in GitHub Desktop.
This program deletes EVERYTHING in a folder and all subfolders, including files with corrupted (or too long) filenames, which is why I created it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This Python script is designed to delete all files and directories in a specified folder. | |
# It handles files with special characters in their names and long paths using UNC paths. | |
# | |
# Instructions: | |
# 1. Change the 'folder_to_delete' variable to the path of the directory you want to clear. | |
# 2. Run this script with Python. Ensure you have the necessary permissions to delete the files. | |
# 3. Use this script carefully, as it will permanently delete files and directories. | |
import os | |
import shutil | |
# Change this path to the directory you want to delete | |
folder_to_delete = "C:/temp/_Test" | |
def delete_files(folder_path): | |
""" | |
Deletes all files and directories in the specified folder path, including handling special characters. | |
Args: | |
folder_path (str): The path to the directory where files and subdirectories will be deleted. | |
""" | |
# UNC_PREFIX is a string used to handle files with special characters in paths on Windows. | |
# The prefix '\\?\' tells Windows to turn off all path parsing and allows for longer paths. | |
UNC_PREFIX = "\\\\?\\" | |
# Convert the given folder path to an absolute path and prepend with UNC_PREFIX to handle special characters. | |
folder_path = UNC_PREFIX + os.path.abspath(folder_path) | |
# os.walk is used to iterate over each directory and subdirectory in the folder. | |
# 'topdown=False' makes os.walk start from innermost directories, making it easier to delete subdirectories. | |
for root, dirs, files in os.walk(folder_path, topdown=False): | |
# Iterate over each file in the current directory ('root') | |
for name in files: | |
file_path = os.path.join(root, name) # Create the full path of the file | |
try: | |
os.remove(file_path) # Try to delete the file | |
print(f"Deleted file: {file_path}") | |
except Exception as e: | |
# If there's an error during deletion, print an error message. | |
print(f"Failed to delete file: {file_path}, error: {e}") | |
# Iterate over each directory in the current directory | |
for name in dirs: | |
dir_path = os.path.join(root, name) # Create the full path of the directory | |
try: | |
shutil.rmtree(dir_path) # Try to delete the directory and all its contents | |
print(f"Deleted directory: {dir_path}") | |
except Exception as e: | |
# If there's an error during deletion, print an error message. | |
print(f"Failed to delete directory: {dir_path}, error: {e}") | |
# Example usage of the function: | |
delete_files(folder_to_delete) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment