analitics

Pages

Showing posts with label shutil. Show all posts
Showing posts with label shutil. Show all posts

Wednesday, March 22, 2023

Python 3.11.0 : clean from frequent folder and the list of recent files.

This python script that clears all entries in the Windows File Explorer from frequent folder and the list of recent files:
import os
import shutil

# Quick Access folder path on Windows
quick_access_path = os.path.join(os.environ['USERPROFILE'], 'AppData', 'Roaming', 'Microsoft', 'Windows', 'Recent', 'AutomaticDestinations')

# List all files in the Quick Access folder
files = os.listdir(quick_access_path)
print(files)
# Loop through all files in the Quick Access folder
for file in files:
    # Check if the file name contains "tmp" or "temp"
    if 'tmp' in file.lower() or 'temp' in file.lower():
        # Construct the full file path
        file_path = os.path.join(quick_access_path, file)
        # Delete the file
        os.remove(file_path)
        # Print a message to the console
        print(f"{file_path} deleted successfully.")

# Clear Frequent folder
frequent_folder = os.path.join(os.environ['APPDATA'], 'Microsoft', 'Windows', 'Recent', 'AutomaticDestinations')
os.system('del /f /q "{}\*"'.format(frequent_folder))

# Clear Recent files list
recent_folder = os.path.join(os.environ['APPDATA'], 'Microsoft', 'Windows', 'Recent')
os.system('del /f /q "{}\*"'.format(recent_folder))

Sunday, October 21, 2018

The shutil python module.

The shutil module helps you to accomplish tasks, such as: copying, moving, or removing directory trees.
This python script creates a zip file in the current directory containing all contents of dir and then clears dir.

import shutil
from os import makedirs

def zip(out_fileName, dir):
shutil.make_archive(str(out_fileName), 'zip', dir)
shutil.rmtree(dir)
makedirs(dir[:-1])