I have this python code:
import os
import shutil
# Function to get the total size of a directory including subdirectories
def get_directory_size(path):
total_size = 0
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
filepath = os.path.join(dirpath, filename)
total_size += os.path.getsize(filepath)
return total_size
# Function to list pip.log files bigger than 0.1 GB with their sizes
def list_large_pip_log_files_with_size(path, size_threshold):
large_pip_log_files = []
try:
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
if filename == "pip.log":
filepath = os.path.join(dirpath, filename)
file_size = os.path.getsize(filepath)
if file_size > size_threshold: # 0.1 GB in bytes
large_pip_log_files.append((filepath, file_size))
except FileNotFoundError:
pass
return large_pip_log_files
# Function to list first-level subdirectories bigger than 0.1 GB with their sizes
def list_large_first_level_subdirectories_with_size(path, size_threshold):
large_subdirectories = []
try:
for dirname in os.listdir(path):
dir_full_path = os.path.join(path, dirname)
if os.path.isdir(dir_full_path):
dir_size = get_directory_size(dir_full_path)
if dir_size > size_threshold: # 0.1 GB in bytes
large_subdirectories.append((dir_full_path, dir_size))
except FileNotFoundError:
pass
return large_subdirectories
# Function to delete selected files/directories
def delete_items(items_to_delete):
deleted_size = 0
for item_path, item_size in items_to_delete:
try:
if os.path.isfile(item_path):
os.remove(item_path)
elif os.path.isdir(item_path):
shutil.rmtree(item_path)
deleted_size += item_size
print(f"Deleted: {item_path} ({item_size / (1024 * 1024 * 1024):.2f} GB)")
except Exception as e:
print(f"Error deleting {item_path}: {str(e)}")
return deleted_size
# Input paths
pip_log_path = "C:/your_pip_log_directory"
subdirectories_paths = [
"C:/subdirectory_path1",
"C:/subdirectory_path2",
# Add more paths as needed
]
# File size threshold (0.1 GB)
size_threshold = 0.1 * 1024 * 1024 * 1024 # 0.1 GB in bytes
# Check if it's a dry run
dry_run = input("Is this a dry run? (y/n): ").strip().lower() == "y"
# List large pip.log files
large_pip_log_files = list_large_pip_log_files_with_size(pip_log_path, size_threshold)
# List large first-level subdirectories
large_first_level_subdirectories = []
for path in subdirectories_paths:
large_first_level_subdirectories.extend(list_large_first_level_subdirectories_with_size(path, size_threshold))
# Combine and sort the lists by size in descending order
all_large_items = large_pip_log_files + large_first_level_subdirectories
all_large_items.sort(key=lambda x: x[1], reverse=True)
# Display the list to the user
print("Large pip.log Files and First-Level Subdirectories to Delete:")
for i, (item_path, item_size) in enumerate(all_large_items):
print(f"{i+1}: {item_path} ({item_size / (1024 * 1024 * 1024):.2f} GB)")
# Ask the user for items to delete
if not dry_run:
user_input = input("Enter numbers of items to delete (separated by space), or 0 to exit: ")
items_to_delete_indices = [int(i) - 1 for i in user_input.split() if i.isnumeric()]
I have problem when I'm trying to pass this path: r'F:\temp\.clone - sb'
when it reaches this code:
for dirname in os.listdir(path):
is throw FileNotFoundError
exception. The folder exists for sure and I'm able to get there and its sub directories.
I tried few escape chars, but nothing helped. any idea?
BTW, I debugged the code, and this is what python sees: 'F:\\temp\\.clone - sb'
I've found the solution. I opened project from this path, and I saw that python reads it as: F:\temp\.clone-sb
. It turns out python remove by himself the spaces. So after updating the path in my code for that, it worked like charm