
Python One-Liners That Replace Entire Shell Scripts
I used to write 50-line bash scripts for things Python does in one line. Here are 15 one-liners I use daily. File Operations Start a web server (current directory) python3 -m http.server 8080 Replaces: nginx config + setup for quick file sharing. Find duplicate files python3 - c " import os,hashlib; seen={}; [print(f ' DUPE: {f} ' ) for r,d,files in os.walk( ' . ' ) for f in files if (h:=hashlib.md5(open(os.path.join(r,f), ' rb ' ).read()).hexdigest()) in seen or seen.update({h:f}).__class__.__name__== ' NoneType ' and False] " Okay that's ugly. Here's the readable version: python3 << ' EOF ' import os , hashlib seen = {} for root , dirs , files in os . walk ( ' . ' ): for name in files : path = os . path . join ( root , name ) file_hash = hashlib . md5 ( open ( path , ' rb ' ). read ()). hexdigest () if file_hash in seen : print ( f ' DUPE: { path } == { seen [ file_hash ] } ' ) else : seen [ file_hash ] = path EOF Pretty-print JSON cat ugly.json | python3 -m json.tool Replaces: jq (w
Continue reading on Dev.to Tutorial
Opens in a new tab




