diff --git a/README.md b/README.md index 68d947d..fd0d476 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,10 @@ - [TCP Server](https://lucas-six.github.io/python-cookbook/cookbook/core/asyncio/tcp_server) ([Low-Level APIs](https://lucas-six.github.io/python-cookbook/cookbook/core/asyncio/tcp_server_low)) - [TCP Client](https://lucas-six.github.io/python-cookbook/cookbook/core/asyncio/tcp_client) ([Low-Level APIs](https://lucas-six.github.io/python-cookbook/cookbook/core/asyncio/tcp_client_low)) +### System + +- [Run Command: `subprocess`](https://lucas-six.github.io/python-cookbook/cookbook/core/system/subprocess) + ## Build (构建) ### Command-Line Arguments Parser diff --git a/cookbook/core/system/subprocess.md b/cookbook/core/system/subprocess.md new file mode 100644 index 0000000..1a1beb7 --- /dev/null +++ b/cookbook/core/system/subprocess.md @@ -0,0 +1,26 @@ +# Run command: `subprocess` + +## Recipes + +```python +import subprocess + + +try: + p = subprocess.run(['ls', '-l'], + check=True, + timeout=1.0, + capture_output=True, + text=True, + encoding='utf-8', + errors='strict') +except subproess.TimeoutExpired: + logging.error('timeout') +except CalledProcessError: + logging.error('run failed') +isinstance(p.stdout, str) +``` + +## References + +- [Python - `subprocess` module](https://docs.python.org/3/library/subprocess.html)