Skip to content

FILE ORGANIZATION/SORTING

Python
#!/usr/bin/env python3
"""FILE ORGANIZATION/SORTING"""
import os, tempfile
temp = tempfile.gettempdir()
test_dir = os.path.join(temp, "org_test")
os.makedirs(test_dir, exist_ok=True)
files = ["report.pdf", "photo.jpg", "script.py", "data.csv", "doc.txt"]
for f in files:
    open(os.path.join(test_dir, f), 'w').write("test")
by_ext = {}
for file in os.listdir(test_dir):
    ext = os.path.splitext(file)[1] or ".no_ext"
    by_ext.setdefault(ext, []).append(file)
print("Files organized by extension:")
for ext, files_list in sorted(by_ext.items()):
    print(f"  {ext}: {files_list}")
import shutil
shutil.rmtree(test_dir)