This question already has an answer here:
这个问题在这里已有答案:
- How to just call a command and not get its output [duplicate] 4 answers
如何只是调用一个命令,而不是得到它的输出[重复] 4个答案
For the following command:
对于以下命令:
subprocess.call(shlex.split(
"""/usr/local/itms/bin/iTMSTransporter -m lookupMetadata
-apple_id %s -destination %s"""%(self.apple_id, self.destination))
It prints the entire output into the Terminal window. How would I suppress ALL output here? I tried doing subprocess.call(shlex.split(<command> > /dev/null 2&1
)), but it didn't produce the required results. How would I do this here?
它将整个输出打印到终端窗口。我如何在这里抑制所有输出?我尝试过subprocess.call(shlex.split(
2 个解决方案
#1
37
You can use the stdout=
and stderr=
parameters to subprocess.call()
to direct stdout
or stderr
to a file descriptor of your choice. So maybe something like this:
您可以使用stdout =和stderr =参数来subprocess.call()将stdout或stderr定向到您选择的文件描述符。也许是这样的:
import os
devnull = open(os.devnull, 'w')
subprocess.call(shlex.split(
'/usr/local/itms/bin/iTMSTransporter -m lookupMetadata '
'-apple_id %s -destination %s' % (self,apple_id, self.destination)),
stdout=devnull, stderr=devnull)
Using subprocess.PIPE
, if you're not reading from the pipe, could cause your program to block if it generates a lot of output.
使用subprocess.PIPE,如果你没有从管道中读取,可能会导致程序在生成大量输出时阻塞。
Update
As @yanlend mentions in a comment, newer (3.x) versions of Python include subprocess.DEVNULL
to solve this problem in a more convenient and portable fashion. In that case, the code would look like:
正如@yanlend在评论中提到的,较新的(3.x)版本的Python包括subprocess.DEVNULL以更方便和便携的方式解决这个问题。在这种情况下,代码看起来像:
subprocess.call(shlex.split(
'/usr/local/itms/bin/iTMSTransporter -m lookupMetadata '
'-apple_id %s -destination %s' % (self,apple_id, self.destination)),
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
#2
3
What worked for me is appending 2>/dev/null
at the end of the command.
对我有用的是在命令末尾附加2> / dev / null。
#1
37
You can use the stdout=
and stderr=
parameters to subprocess.call()
to direct stdout
or stderr
to a file descriptor of your choice. So maybe something like this:
您可以使用stdout =和stderr =参数来subprocess.call()将stdout或stderr定向到您选择的文件描述符。也许是这样的:
import os
devnull = open(os.devnull, 'w')
subprocess.call(shlex.split(
'/usr/local/itms/bin/iTMSTransporter -m lookupMetadata '
'-apple_id %s -destination %s' % (self,apple_id, self.destination)),
stdout=devnull, stderr=devnull)
Using subprocess.PIPE
, if you're not reading from the pipe, could cause your program to block if it generates a lot of output.
使用subprocess.PIPE,如果你没有从管道中读取,可能会导致程序在生成大量输出时阻塞。
Update
As @yanlend mentions in a comment, newer (3.x) versions of Python include subprocess.DEVNULL
to solve this problem in a more convenient and portable fashion. In that case, the code would look like:
正如@yanlend在评论中提到的,较新的(3.x)版本的Python包括subprocess.DEVNULL以更方便和便携的方式解决这个问题。在这种情况下,代码看起来像:
subprocess.call(shlex.split(
'/usr/local/itms/bin/iTMSTransporter -m lookupMetadata '
'-apple_id %s -destination %s' % (self,apple_id, self.destination)),
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
#2
3
What worked for me is appending 2>/dev/null
at the end of the command.
对我有用的是在命令末尾附加2> / dev / null。