
How to Use Python to Monitor Files and Processes With psutil
Monitor processes, memory, disk and open files from Python with psutil: list top processes, check a service, watch CPU and alert on thresholds.
Tag
30 posts

Monitor processes, memory, disk and open files from Python with psutil: list top processes, check a service, watch CPU and alert on thresholds.

Parse CSV and Excel files in Python to build reports: csv.DictReader, grouping totals, writing a summary, handling encodings, and reading xlsx with openpyxl.

Query JSON in Python the way jq does: load, walk nested keys safely, filter lists, group and count, and handle missing keys without KeyError.

Fix a Python subprocess that hangs: pipe buffer deadlocks with wait, missing communicate, stdin prompts, timeouts, and killing process groups.

Use Python Pexpect to spawn programs, wait for prompts, send replies, handle timeouts and EOF, run SSH commands, and log the session for debugging.

Use python -c one liners from the shell: format JSON, encode and decode, do math, generate passwords and serve files, with quoting tips for Bash.

Replace a Bash deploy script with Python: subprocess with check=True, release directories and symlink swaps, health checks, rollback and clear logging.

Replace a Bash backup script with Python: pathlib, tarfile, timestamped names, retention, error handling and logging, with a note on when Bash is enough.

Test a Python command line script with pytest: call main() directly, use capsys and tmp_path, check exit codes, and run the real entry point with subprocess.

Handle Python CalledProcessError from subprocess.run: read returncode, stdout and stderr, understand exit codes, and decide when to use check.

Compare subprocess, the sh library and plumbum for running commands in Python: syntax, errors, piping, portability and security, with the same task in each.

Compare Click, Typer and argparse for Python command-line tools: size, type hints, subcommands, dependencies and testing, with the same example in each.