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 main function.
run_cmd_stream
Some commands take a long time to complete. If we want to see live stream output (one line at a time), run_cmd_stream can be used.
from python_run_cmd import run_cmd_stream
run_cmd_stream("ping -n 100 baidu.com")
We won't have to wait until all 100 pings to complete to see the output.
Metadata
Release files for python-run-cmd 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| python_run_cmd-0.1.1.tar.gz | 4.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| python_run_cmd-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.3 kB
Release files / python_run_cmd-0.1.1.tar.gz
| Download URL | python_run_cmd-0.1.1.tar.gz |
|---|---|
| Size | 4.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6cd114c322639689d0d39ba5a18395fa4ab0631172358a9ab26ff5577e829348
|
|
BLAKE2b-256 checksum How to use checksums |
d46396aaf3cb896b7cf0564d04f4efc3d0cd305dd43dbe4cd7e9e33c35915d83
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.8.10 Windows/10
|
Release files / python_run_cmd-0.1.1-py3-none-any.whl
| Download URL | python_run_cmd-0.1.1-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d03af62cb95eb492d827bb1cacaa0a22a17e70f5ad6d9599b1951b67df2e1d4a
|
|
BLAKE2b-256 checksum How to use checksums |
85126b6f303a860cb2fcf6990d365c7c25ac74fc1915610e5a6b23846c8eb74a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.8.10 Windows/10
|