
Python One-Liners That Save Me Hours Every Week
Tiny Scripts, Big Time Savings These are the Python one-liners I actually use in production — not clever tricks for interviews, but practical shortcuts that save real time. File Operations # Read entire file content = open ( " data.txt " ). read () # Read lines into list (stripped) lines = [ l . strip () for l in open ( " data.txt " )] # Write list to file open ( " output.txt " , " w " ). write ( " \n " . join ( lines )) # Count lines in a file line_count = sum ( 1 for _ in open ( " huge_file.log " )) # Find all .py files recursively from pathlib import Path py_files = list ( Path ( " . " ). rglob ( " *.py " )) Data Processing # CSV to list of dicts import csv data = list ( csv . DictReader ( open ( " data.csv " ))) # JSON file to dict import json config = json . loads ( open ( " config.json " ). read ()) # Flatten nested list flat = [ x for sublist in nested for x in sublist ] # Remove duplicates preserving order unique = list ( dict . fromkeys ( items )) # Group items from itertools
Continue reading on Dev.to Tutorial
Opens in a new tab




