Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. import subprocess
  2.  
  3. def shellex(cmd, directory, capture=False):
  4. '''
  5. Shell Execute:
  6. Execute any system shell command as non-blocking subprocess.
  7.  
  8. Arguments:
  9. command: str: any shell command
  10. directory: str: directory where the subprocess will launch
  11. capture: boolean: store the stdout in a variable for further processing
  12.  
  13. Example Usage:
  14. execute('ls -a', '/')
  15. execute('afplay music.mp3', '$HOME)
  16. execute("open -a 'Sublime Text' my_script.py" 'home/Documents')
  17. '''
  18. with subprocess.Popen(cmd.split(' '), cwd=directory, stdout=subprocess.PIPE) as process:
  19. stdout, _ = process.communicate()
  20. if not capture:
  21. print(stdout.decode('utf-8'))
  22. return stdout.decode('utf-8')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement