1. The Script
----------------------------------
#!/usr/bin/python
import subprocess
def getCallResult(cmdARGS):
fd_popen = subprocess.Popen(cmdARGS.split(), stdout=subprocess.PIPE).stdout
data = fd_popen.read().strip()
fd_popen.close()
return data
data = getCallResult("ls -la")
print (data)
----------------------------------
2. Result Example
----------------------------------
# python test.py
total 524
drwxrwxr-x 4 parkmo parkmo 4096 1월 27 16:30 .
drwxrwxr-x 45 parkmo parkmo 4096 1월 27 11:59 ..
drw-rw-r-- 1 parkmo parkmo 421 1월 27 12:01 test.py
..
..
1) If you don't want use split() when call subprocess.Popen, use [ ].
subprocess.Popen(["ls", "-la"], stdout=subprocess.PIPE).stdout
2) If you can want to file descriptor.
fd_popen = subprocess.Popen(cmdARGS.split(), stdout=subprocess.PIPE).wait()
You can use Blocking or Non-Blocking.
Test Environment: Python 2.4.x
Caution: On Python 3.x, when you want to use print function, you can not use print. You must write "print(....)"
Comments
Post a Comment