sh - executing system commands and applications in Python

Check out the new site at https://rkblog.dev.

sh is a wrapper around Python subprocess library. It offers a nice and easy to use API - it allows using programs or system commands like if they were a function.

The installation is typical: pip install sh. After installation you can import any system command or application from the sh module:

from sh import cd, ls, wesnoth

cd('/var/log')
print ls('./')
print wesnoth('--version')
Arguments can also be passed as methods:
from sh import cd, ls

cd('/var/log')
print ls.nginx('./')
from sh import git
git.checkout("master")

If you want to execute a command in background then use _bg=True. Data passed to standard output or error can be redirected to a file or function passed by using _out and _err arguments. You can also controll interactive commands.

RkBlog

Python programming, 26 December 2012


Check out the new site at https://rkblog.dev.
Comment article
Comment article RkBlog main page Search RSS Contact