python -- subprocess

来源:互联网 发布:淘宝网渔具 编辑:程序博客网 时间:2024/06/09 16:37

subprocess用于替换一下包的功能

os.system
os.spawn*
os.popen*
popen2.*
commands.*


subprocess支持的接口

1. subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)


2. subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False)


3.subprocess.check_output(args,*,stdin=None,stderr=None,sal_newlines=False)

4.class subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None,preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, uni-versal_newlines=False, startupinfo=None, creationflags=0)


Popen对象


Popen.poll()

Check if child process has terminated. Set and return returncode attribute.


Popen.communicate(input=None)
Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for
process to terminate. The optional input argument should be a string to be sent to the child process, or None, if
no data should be sent to the child.


Popen.send_signal(signal)
Sends the signal signal to the child.


Popen.terminate()
Stop the child. On Posix OSs the method sends SIGTERM to the child. On Windows the Win32 API function
TerminateProcess() is called to stop the child.


Popen.kill()
Kills the child. On Posix OSs the function sends SIGKILL to the child. On Windows kill() is an alias for
terminate().


Popen.stdin
If the stdin argument was PIPE, this attribute is a file object that provides input to the child process. Otherwise,
it is None.


Popen.stdout
If the stdout argument was PIPE, this attribute is a file object that provides output from the child process.
Otherwise, it is None.

Popen.wait()
Wait for child process to terminate. Set and return returncode attribute.


Popen.stderr
If the stderr argument was PIPE, this attribute is a file object that provides error output from the child process.
Otherwise, it is None.


Popen.pid
The process ID of the child process.
Note that if you set the shell argument to True, this is the process ID of the spawned shell.


Popen.returncode
The child return code, set by poll() and wait() (and indirectly by communicate()). A None value
indicates that the process hasn’t terminated yet.


例子:

>>> p = subprocess.Popen("aplay -D plughw:1,2 /home/tingzhi.wav", shell=True,stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
>>> print p.returncode
None
>>> out,err = p.communicate()
>>> print p.returncode
1
>>> print out
aplay: main:722: 音乐打开错误: 没有那个文件或目录
>>> print err
None
>>> print p.wait()
1
>>>




0 0
原创粉丝点击