Created
August 25, 2013 07:48
-
-
Save suzaku/6332533 to your computer and use it in GitHub Desktop.
system in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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