{"id":13,"description":"Python - process shell command result line by line","tags":["python","programming","shell","snippets"],"contents":["\u003e\u003e\u003e import subprocess","\u003e\u003e\u003e cmd_result = subprocess.run(\"ls -l\".split(), text=True, capture_output=True)","\u003e\u003e\u003e # split the output by newline to process each line","\u003e\u003e\u003e for line in cmd_result.stdout.split(\"\n\"):","...     if line.startswith('d'):","...             # processing only directories","...             print(line)","...","drwxr-xr-x 1 root root 4096 Nov  9 08:42 man1","drwxr-xr-x 2 root root 4096 Nov  9 08:42 man5","drwxr-xr-x 2 root root 4096 Nov  9 08:07 man8"]}
