Python 官方文档:入门教程 => 点击学习
python2.7 源码中的注释(由于能力有限,翻译的不太准确): 这个模块允许您开启进程、连接输入、输出和错误的管道,并获取他们的返回代码。这个模块计划替代一些旧代码,如: os.system、os.spawn*、os.Popen、po
python2.7 源码中的注释(由于能力有限,翻译的不太准确):
这个模块允许您开启进程、连接输入、输出和错误的管道,并获取他们的返回代码。这个模块计划替代一些旧代码,如:
os.system、os.spawn*、os.Popen、popen2.* 、commands.*
关于subprocess模块可以用来取代这些模块和功能在下面可以找到
这个模块定义了一个Popen的类:
参数为:
args应该是一个字符串或序列的程序命令及参数。程序通常执行序列或字符串的第一项,但可以通过使用明确的参数进行设置。
在UNIX上,shell = False(默认):在这种情况下,Popen类使用os.execvp()来执行程序的子进程。args应该通常是一个序列。字符串将被视为只有一个字符串的序列(程序执行)。
- 替代 /bin/sh shell 的引号部分
- ---------------------------------
- output=`mycmd myarg`
- ==>
- output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0]
-
-
- 替代 shell 的管道
- -------------------------
- output=`dmesg | grep hda`
- ==>
- p1 = Popen(["dmesg"], stdout=PIPE)
- p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
- output = p2.communicate()[0]
-
-
- 替代 os.system()
- ---------------------
- sts = os.system("mycmd" + " myarg")
- ==>
- p = Popen("mycmd" + " myarg", shell=True)
- pid, sts = os.waitpid(p.pid, 0)
-
- 注意:
-
- * 通过shell调用程序通常不是必须的
-
- * 查看returncode attribute要比exitstatus容易些.
-
- 一个更现实的例子:
-
- try:
- retcode = call("mycmd" + " myarg", shell=True)
- if retcode < 0:
- print >>sys.stderr, "Child was terminated by signal", -retcode
- else:
- print >>sys.stderr, "Child returned", retcode
- except OSError, e:
- print >>sys.stderr, "Execution failed:", e
-
-
- 替代 os.spawn*
- -------------------
- P_NOWAIT example:
-
- pid = os.spawNLP(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg")
- ==>
- pid = Popen(["/bin/mycmd", "myarg"]).pid
-
-
- P_WAIT example:
-
- retcode = os.spawnlp(os.P_WAIT, "/bin/mycmd", "mycmd", "myarg")
- ==>
- retcode = call(["/bin/mycmd", "myarg"])
-
-
- Vector example:
-
- os.spawnvp(os.P_NOWAIT, path, args)
- ==>
- Popen([path] + args[1:])
-
-
- Environment example:
-
- os.spawnlpe(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg", env)
- ==>
- Popen(["/bin/mycmd", "myarg"], env={"PATH": "/usr/bin"})
-
-
- 替代 os.popen*
- -------------------
- pipe = os.popen("cmd", mode='r', bufsize)
- ==>
- pipe = Popen("cmd", shell=True, bufsize=bufsize, stdout=PIPE).stdout
-
- pipe = os.popen("cmd", mode='w', bufsize)
- ==>
- pipe = Popen("cmd", shell=True, bufsize=bufsize, stdin=PIPE).stdin
-
-
- (child_stdin, child_stdout) = os.popen2("cmd", mode, bufsize)
- ==>
- p = Popen("cmd", shell=True, bufsize=bufsize,
- stdin=PIPE, stdout=PIPE, close_fds=True)
- (child_stdin, child_stdout) = (p.stdin, p.stdout)
-
-
- (child_stdin,
- child_stdout,
- child_stderr) = os.popen3("cmd", mode, bufsize)
- ==>
- p = Popen("cmd", shell=True, bufsize=bufsize,
- stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
- (child_stdin,
- child_stdout,
- child_stderr) = (p.stdin, p.stdout, p.stderr)
-
-
- (child_stdin, child_stdout_and_stderr) = os.popen4("cmd", mode,
- bufsize)
- ==>
- p = Popen("cmd", shell=True, bufsize=bufsize,
- stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
- (child_stdin, child_stdout_and_stderr) = (p.stdin, p.stdout)
-
- 在 Unix系统中, os.popen2, os.popen3 与 os.popen4 同样可以在没有shell介入的情况下直接传递给程序
- 以序列形式执行命令行
- 这种方法可以用下面的方法替换:
-
- (child_stdin, child_stdout) = os.popen2(["/bin/ls", "-l"], mode,
- bufsize)
- ==>
- p = Popen(["/bin/ls", "-l"], bufsize=bufsize, stdin=PIPE, stdout=PIPE)
- (child_stdin, child_stdout) = (p.stdin, p.stdout)
-
- Return code handling translates as follows:
-
- pipe = os.popen("cmd", 'w')
- ...
- rc = pipe.close()
- if rc is not None and rc % 256:
- print "There were some errors"
- ==>
- process = Popen("cmd", 'w', shell=True, stdin=PIPE)
- ...
- process.stdin.close()
- if process.wait() != 0:
- print "There were some errors"
-
-
- 替代 popen2.*
- ------------------
- (child_stdout, child_stdin) = popen2.popen2("somestring", bufsize, mode)
- ==>
- p = Popen(["somestring"], shell=True, bufsize=bufsize
- stdin=PIPE, stdout=PIPE, close_fds=True)
- (child_stdout, child_stdin) = (p.stdout, p.stdin)
-
- 在 Unix系统中, popen2 也可以在没有shell介入的情况下直接传递给程序以序列形式执行命令行.
- 这种方法可以用下面的方法替换:
-
- (child_stdout, child_stdin) = popen2.popen2(["mycmd", "myarg"], bufsize,
- mode)
- ==>
- p = Popen(["mycmd", "myarg"], bufsize=bufsize,
- stdin=PIPE, stdout=PIPE, close_fds=True)
- (child_stdout, child_stdin) = (p.stdout, p.stdin)
-
- The popen2.Popen3 and popen2.Popen4 basically works as subprocess.Popen,
- except that:
-
- * subprocess.Popen raises an exception if the execution fails
- * the capturestderr argument is replaced with the stderr argument.
- * stdin=PIPE and stdout=PIPE must be specified.
- * popen2 closes all filedescriptors by default, but you have to specify
- close_fds=True with subprocess.Popen.
--结束END--
本文标题: python中的subprocess
本文链接: https://lsjlt.com/news/190957.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0