Refactor Python
Refactor this Bash script to improve error handling, readability, and maintainability.