Skip to content

Instantly share code, notes, and snippets.

@suzaku
Created August 25, 2013 07:48
Show Gist options
  • Save suzaku/6332533 to your computer and use it in GitHub Desktop.
Save suzaku/6332533 to your computer and use it in GitHub Desktop.
system in Python
import os
import sys
def my_system(command):
pid = os.fork()
if pid == -1:
return -1
elif pid == 0:
os.execv('/bin/sh', ['/bin/sh', '-c', command])
sys.exit(-1) # in case execv failed
_, st = os.waitpid(pid, 0)
return st
if __name__ == '__main__':
print my_system('ls -lh')
print my_system('python -c "import this"')
print my_system('dontexist')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment