python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令

2023-09-02Python开发问题
3

本文介绍了python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

用于识别 Xcode 是否在 Mac 上运行的命令框架:cmd = "ps -ax | grep -v grep | grep Xcode"

Command framed to identify if Xcode is running on Mac: cmd = "ps -ax | grep -v grep | grep Xcode"

如果 Xcode 没有运行,那么上面的命令适用于 subprocess 模块的 Popen 方法,但会引发 CalledProcessErrorcheck_output 方法.我试图通过以下代码检查stderr,但未能获得适当的信息来理解原因.

If Xcode is not running, then above command works well with Popen method of subprocess module, but raises a CalledProcessError with check_output method. I tried to inspect the stderr through the following code, but failed to get appropriate information to understand the reason.

from subprocess import check_output, STDOUT, CalledProcessError

psCmd = "ps -ax | grep -v grep | grep Xcode"
o = None
try:
    o = check_output(psCmd, stderr=STDOUT, shell=True)
except CalledProcessError as ex:
    print 'Error:', ex, o

异常信息如下:

错误:命令'ps -ax |grep -v grep |grep Xcode' 返回非零退出状态 1 无

问题:为什么上面的命令对 Popen 有效,但由于 check_output 失败?

Question: Why the above command works with Popen, but fails with check_output ?

注意:如果 Xcode 正在运行,命令对这两种方法都适用.

Note: Command works well with both approach, if Xcode is running.

推荐答案

check_output() 按预期工作.下面是它在 Popen() 方面的简化实现:

check_output() works as expected. Here's its simplified implementation in terms of Popen():

def check_output(cmd):
    process = Popen(cmd, stdout=PIPE)
    output = process.communicate()[0]
    if process.returncode != 0:
        raise CalledProcessError(process.returncode, cmd, output=output)
    return output

grep 如果没有找到任何东西,则返回 1,也就是说,如果 Xcode 没有运行,您应该期待异常.

grep returns 1 if it hasn't found anything i.e., you should expect the exception if Xcode is not running.

注意:如实现所示,即使发生异常也能得到输出:

Note: as the implementation shows, you can get the output even if the exception occurs:

#!/usr/bin/env python
from subprocess import check_output, STDOUT, CalledProcessError

cmd = "ps -ax | grep -v grep | grep Xcode"
try:
    o = check_output(cmd, stderr=STDOUT, shell=True)
    returncode = 0
except CalledProcessError as ex:
    o = ex.output
    returncode = ex.returncode
    if returncode != 1: # some other error happened
        raise

您可以改用 pgrep -a Xcode 命令(注意:以 p 开头)或使用 psutil 模块作为可移植代码:

You could probably use pgrep -a Xcode command instead (note: starts with p) or use psutil module for a portable code:

#!/usr/bin/env python
import psutil # $ pip install psutil

print([p.as_dict() for p in psutil.process_iter() if 'Xcode' in p.name()])

这篇关于python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

pandas 有从特定日期开始的按月分组的方式吗?
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
2024-08-22 Python开发问题
10

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10