Jul-17-2024, 08:58 PM
(This post was last modified: Jul-17-2024, 09:04 PM by Gribouillis.)
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:
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]
Gribouillis write Jul-17-2024, 09:04 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
