您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

抑制python子进程调用中的输出

抑制python子进程调用中的输出

您可以使用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)

subprocess.PIPE如果不从管道读取数据,则使用,可能会导致程序在生成大量输出时阻塞。

正如@yanlend在评论中提到的那样,Python的更新版本(3.x)包括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)
python 2022/1/1 18:46:54 有333人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶