Python Forum
Windows Disk Cleanup Code Help Needed - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Windows Disk Cleanup Code Help Needed (/thread-42467.html)



Windows Disk Cleanup Code Help Needed - nal2us2 - Jul-17-2024

Hi all.

I am trying to generate Python code to do the following:
Access the Windows Disk Cleanup program and clear out the regular and system files with full admin rights, and also without having the Python pop up window occur during the program execution.

The following is what I have so far, (when running the program it says completed but when I open the Disk Cleanup app and check.... nothing has been erased. I also get an Access is denined message as well. Any help would be greatly appreciated in modifying the following code to work as intended in the above-mentioned:

import os
import hashlib
import time

class Duplython:
    def __init__(self):
        self.home_dir = os.getcwd()
        self.File_hashes = []
        self.Cleaned_dirs = []
        self.Total_bytes_saved = 0
        self.block_size = 65536
        self.count_cleaned = 0

    def welcome(self) -> None:
        print("**************** DUPLYTHON ****************************")
        print("Cleaning...")
        time.sleep(3)

    def generate_hash(self, filename: str) -> str:
        filehash = hashlib.sha256()
        try:
            with open(filename, "rb") as file:
                file_block = file.read(self.block_size)
                while len(file_block) > 0:
                    filehash.update(file_block)
                    file_block = file.read(self.block_size)
            filehash = filehash.hexdigest()
            return filehash
        except:
            return False

    def clean(self) -> None:
        all_dirs = [path for path, _, _ in os.walk(".")]
        for path in all_dirs:
            os.chdir(path)
            all_files = [file for file in os.listdir() if os.path.isfile(file)]
            for file in all_files:
                file_hash = self.generate_hash(file)
                if file_hash not in self.File_hashes:
                    if file_hash:
                        self.File_hashes.append(file_hash)
                else:
                    byte_saved = os.path.getsize(file)
                    self.count_cleaned += 1
                    self.Total_bytes_saved += byte_saved
                    os.remove(file)
                    filename = file.split("/")[-1]
                    print(f"{filename} cleaned")
            os.chdir(self.home_dir)

if __name__ == "__main__":
    App = Duplython()
    App.welcome()
    App.clean()[quote][/quote]



RE: Windows Disk Cleanup Code Help Needed - deanhystad - Jul-18-2024

Where do you attempt to access the Windows Disk Cleanup program?


RE: Windows Disk Cleanup Code Help Needed - nal2us2 - Jul-18-2024

(Jul-18-2024, 03:25 PM)deanhystad Wrote: Where do you attempt to access the Windows Disk Cleanup program?

C:\Windows\System32


RE: Windows Disk Cleanup Code Help Needed - deanhystad - Jul-19-2024

Where in your program do you try to use the windows disk cleanup program?