Created
February 2, 2024 21:59
-
-
Save betillogalvanfbc/bf4c2576995e9650c3c376d928c18743 to your computer and use it in GitHub Desktop.
cleanbrowsers.py
This file contains hidden or 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
import os | |
import shutil | |
import subprocess | |
from pathlib import Path | |
browsers_to_kill = [ | |
'chrome.exe', | |
'firefox.exe', | |
'msedge.exe', | |
'brave.exe', | |
'vivaldi.exe', | |
'opera.exe' | |
] | |
browser_paths = { | |
'Chrome': os.path.join(Path.home(), 'AppData', 'Local', 'Google', 'Chrome', 'User Data'), | |
'Firefox': os.path.join(Path.home(), 'AppData', 'Roaming', 'Mozilla', 'Firefox', 'Profiles'), | |
'Edge': os.path.join(Path.home(), 'AppData', 'Local', 'Microsoft', 'Edge', 'User Data'), | |
'Brave': os.path.join(Path.home(), 'AppData', 'Local', 'BraveSoftware', 'Brave-Browser', 'User Data'), | |
'Vivaldi': os.path.join(Path.home(), 'AppData', 'Local', 'Vivaldi', 'User Data'), | |
'Opera': os.path.join(Path.home(), 'AppData', 'Roaming', 'Opera Software', 'Opera Stable'), | |
} | |
def kill_browser_processes(): | |
"""Finaliza los procesos de los navegadores.""" | |
for browser in browsers_to_kill: | |
try: | |
subprocess.run(['taskkill', '/im', browser, '/f'], check=True) | |
print(f"Proceso {browser} terminado.") | |
except subprocess.CalledProcessError as e: | |
print(f"No se pudo terminar el proceso {browser}: {e}") | |
def delete_folder_contents(folder: str): | |
"""Elimina el contenido de una carpeta sin eliminar la carpeta misma.""" | |
for item_name in os.listdir(folder): | |
item_path = os.path.join(folder, item_name) | |
try: | |
if os.path.isfile(item_path) or os.path.islink(item_path): | |
os.unlink(item_path) | |
elif os.path.isdir(item_path): | |
shutil.rmtree(item_path) | |
print(f"Eliminado: {item_path}") | |
except Exception as e: | |
print(f"Error al eliminar {item_path}: {e}") | |
kill_browser_processes() | |
for browser, path in browser_paths.items(): | |
print(f"Limpieza de {browser} iniciada...") | |
if os.path.exists(path): | |
delete_folder_contents(path) | |
else: | |
print(f"La ruta {path} no existe. Es posible que {browser} no esté instalado o la ruta haya cambiado.") | |
print("Limpieza completada.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment