Free Python automation scripts

Copy, save as the file name shown, and run with Python 3.8+. Standard library only, nothing to install. Test on sample files first.

Organize a folder by file type

01_organize_folder.py

"""Sort files in a folder into subfolders by type. Usage: python 01_organize_folder.py FOLDER [--dry-run]"""
import sys, shutil
from pathlib import Path
KINDS = {"Images": {".jpg",".jpeg",".png",".gif",".webp",".svg"}, "Documents": {".pdf",".doc",".docx",".txt",".xlsx",".csv",".pptx"},
         "Archives": {".zip",".tar",".gz",".rar",".7z"}, "Audio": {".mp3",".wav",".m4a"}, "Video": {".mp4",".mkv",".mov"}, "Code": {".py",".js",".html",".css",".json"}}
def main():
    if len(sys.argv) < 2: sys.exit(__doc__)
    root, dry = Path(sys.argv[1]), "--dry-run" in sys.argv
    for f in sorted(root.iterdir()):
        if not f.is_file(): continue
        kind = next((k for k, ext in KINDS.items() if f.suffix.lower() in ext), "Other")
        dest = root / kind / f.name
        print(f"{f.name} -> {kind}/")
        if not dry:
            dest.parent.mkdir(exist_ok=True); shutil.move(str(f), str(dest))
if __name__ == "__main__": main()

Find duplicate files

04_find_duplicate_files.py

"""Find duplicate files by content hash. Usage: python 04_find_duplicate_files.py FOLDER"""
import sys, hashlib
from pathlib import Path
from collections import defaultdict
def sha(p):
    h = hashlib.sha256()
    with open(p, "rb") as f:
        for chunk in iter(lambda: f.read(1 << 20), b""): h.update(chunk)
    return h.hexdigest()
def main():
    if len(sys.argv) < 2: sys.exit(__doc__)
    groups = defaultdict(list)
    for p in Path(sys.argv[1]).rglob("*"):
        if p.is_file(): groups[(p.stat().st_size, sha(p))].append(p)
    dups = [v for v in groups.values() if len(v) > 1]
    for g in dups: print("DUPLICATES:\n  " + "\n  ".join(map(str, g)))
    print(f"{len(dups)} duplicate group(s) found")
if __name__ == "__main__": main()

Check if websites are up

06_site_status.py

"""Check if websites are up and how fast they respond. Usage: python 06_site_status.py URL [URL ...]"""
import sys, time, urllib.request
def main():
    if len(sys.argv) < 2: sys.exit(__doc__)
    for url in sys.argv[1:]:
        t = time.time()
        try:
            with urllib.request.urlopen(urllib.request.Request(url, headers={"User-Agent": "status-check/1.0"}), timeout=10) as r:
                print(f"UP   {r.status}  {(time.time()-t)*1000:6.0f} ms  {url}")
        except Exception as e:
            print(f"DOWN      {url}  ({type(e).__name__}: {e})")
if __name__ == "__main__": main()

Get all 10 scripts, ₹99   More tools

Use website-checking scripts only on sites you own or may test.