Run a command within python
Project description
run-cmd
Run a command within python
Install it
pip install python-run-cmd
# pip install git+https://github.com/ffreemt/run-cmd
# poetry add git+https://github.com/ffreemt/run-cmd
# git clone https://github.com/ffreemt/run-cmd && cd run-cmd
Use it
run_cmd
from python_run_cmd import run_cmd
ret = run_cmd("ls -l")
if ret.returncode:
print("Failed")
else:
print("OK")
An exception will be raised when errors occur. This may come handy sometimes.
from python_run_cmd import run_cmd
try:
ret = run_cmd("lsss -l")
except Exception as exc:
print(exc)
# 'lsss' is not recognized as an internal
# or external command, operable program or batch file.
Set raise_stderr=False
to ignore errors
from python_run_cmd import run_cmd
ret = run_cmd("lsss -l", raise_stderr=False)
if ret.returncode:
print("Failed")
else:
print("OK")
run_cmd_async
Simultaneously run several commands:
import asyncio
import webbrowser
from python_run_cmd import run_cmd_async
async def browse_8000():
webbrowser.open("http://127.0.0.1:8000")
async def main():
return await asyncio.gather(
run_cmd_async("ping www.baidu.com"),
browse_8000(),
run_cmd_async("python -m http.server"),
)
asyncio.run(main())
For Python up to 3.10, it can be simplified (it won't work with Python 3.11):
import asyncio
import webbrowser
from python_run_cmd import run_cmd_async
async def browse_8000():
webbrowser.open("http://127.0.0.1:8000")
asyncio.run(
asyncio.wait(
[
run_cmd_async("ping www.baidu.com"),
browse_8000(),
run_cmd_async("python -m http.server"),
]
)
)
Note there is no auxiliary async function main
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
python_run_cmd-0.1.1a1.tar.gz
(4.1 kB
view hashes)
Built Distribution
Close
Hashes for python_run_cmd-0.1.1a1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c357ff2ec64021e95efbeaeb12d77748ca2f3bbb2dccf10a2f4cfcc72e1d4a7 |
|
MD5 | 76d2781b4560280b573a8b06a19dd2c3 |
|
BLAKE2b-256 | 45042c9d29082a55030c373f0aad5f705441accdcb3c16186880a614be4778f3 |