Many computers, after prolonged use, encounter two common issues—
one is the accumulation of various temporary files that take up space and slow down performance; the other is the presence of numerous duplicate files, where the same photo or video may be stored in several different folders. Manually cleaning these can be tedious, but writing a script in Python can save a lot of trouble.
1. Cleaning Temporary Files
The most common temporary files are <span>.tmp</span>, <span>.log</span>, and files starting with <span>~</span>. Writing a script to traverse the folders and delete them is straightforward:
from pathlib import Path
folder = Path(r"D:\work") # Directory to clean
temp_suffixes = [".tmp", ".log"]
for file in folder.rglob("*"):
if file.is_file() and (file.suffix.lower() in temp_suffixes or file.name.startswith("~")):
try:
file.unlink()
print(f"Deleted: {file}")
except Exception as e:
print(f"Deletion failed: {file}, {e}")
<span>rglob("*")</span> will recursively traverse subfolders, allowing you to find and delete all temporary files in the directory at once.
2. Finding Duplicate Files
The conventional method for identifying duplicate files is:
- 1. First compare file sizes; files with different sizes are definitely not duplicates.
- 2. If sizes are the same, calculate the hash (e.g., MD5); if they match exactly, they are considered duplicates.
import hashlib
from pathlib import Path
from collections import defaultdict
def md5sum(file, chunk_size=8192):
h = hashlib.md5()
with open(file, "rb") as f:
while chunk := f.read(chunk_size):
h.update(chunk)
return h.hexdigest()
folder = Path(r"D:\photos")
hash_map = defaultdict(list)
for file in folder.rglob("*"):
if file.is_file():
file_hash = md5sum(file)
hash_map[file_hash].append(file)
for h, files in hash_map.items():
if len(files) > 1:
print("Found duplicate files:")
for f in files:
print(" ", f)
This will list all duplicate files. You can manually keep one and delete the others, or directly write logic to “keep only the first, delete the rest.”
3. Automatically Deleting Duplicate Files (Optional)
If you are sure you want to automate the cleanup, you can add deletion logic:
for h, files in hash_map.items():
if len(files) > 1:
for f in files[1:]:
f.unlink()
print(f"Deleted duplicate: {f}")
This will keep only the first file in each group and delete the rest.
4. Combining Temporary File and Duplicate File Cleanup
Often, cleaning is done together; you can merge the two logics above to first delete temporary files and then find duplicate files, achieving a thorough cleanup in one go.
5. Precautions
- 1. It is strongly recommended to print before deleting to confirm there are no mistakes before proceeding with deletion.
- 2. Hashing large files is time-consuming; it is advisable to filter by file size first before calculating the hash.
- 3. Do not run this on the system drive; some
<span>.log</span>or cache files are essential for software, so it’s best to only use it in your documents, photos, downloads, etc. - 4. It is advisable to keep a backup in case of accidental deletion.
With this script, the “junk piles” that have accumulated on your computer over the years can be cleaned up, freeing up a significant amount of space.