Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- def shellex(cmd, directory, capture=False):
- '''
- Shell Execute:
- Execute any system shell command as non-blocking subprocess.
- Arguments:
- command: str: any shell command
- directory: str: directory where the subprocess will launch
- capture: boolean: store the stdout in a variable for further processing
- Example Usage:
- execute('ls -a', '/')
- execute('afplay music.mp3', '$HOME)
- execute("open -a 'Sublime Text' my_script.py" 'home/Documents')
- '''
- with subprocess.Popen(cmd.split(' '), cwd=directory, stdout=subprocess.PIPE) as process:
- stdout, _ = process.communicate()
- if not capture:
- print(stdout.decode('utf-8'))
- return stdout.decode('utf-8')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement