相关疑难解决方法(0)

检查CalledProcessError的输出

我正在使用pythons子进程模块中的subprocess.check_output来执行ping命令.我是这样做的:

output = subprocess.check_output(["ping","-c 2 -W 2","1.1.1.1")
Run Code Online (Sandbox Code Playgroud)

它引发了一个CalledProcessError,并说输出是函数的参数之一.任何人都可以帮我如何阅读该输出.我想将输出读入一个字符串并解析它.例如,如果ping返回,请说

100%丢包

我需要抓住它.如果有任何其他更好的方法..请建议.谢谢.

python subprocess

42
推荐指数
3
解决办法
9万
查看次数

如何从Python程序中从命令行获取数据?

我想从python脚本中运行命令行程序并获取输出.

如何获取foo显示的信息,以便我可以在脚本中使用它?

例如,我foo file1从命令行调用它打印出来

Size: 3KB
Name: file1.txt
Other stuff: blah
Run Code Online (Sandbox Code Playgroud)

我怎样才能得到类似的文件名filename = os.system('foo file1')

python command-line pipe

24
推荐指数
2
解决办法
3万
查看次数

当子进程引发CalledProcessError异常时,检查命令的返回码

我想stdout在python(3)脚本中捕获shell命令的流,并且能够同时检查shell命令的返回代码,如果它返回错误(即,如果它的返回代码是不是0).

subprocess.check_output似乎是这样做的合适方法.来自subprocess的手册页:

check_output(*popenargs, **kwargs)
    Run command with arguments and return its output as a byte string.

    If the exit code was non-zero it raises a CalledProcessError.  The
    CalledProcessError object will have the return code in the returncode
    attribute and output in the output attribute.
Run Code Online (Sandbox Code Playgroud)

但是,当它失败时,我没有成功从shell命令获取返回码.我的代码看起来像这样:

import subprocess
failing_command=['ls', 'non_existent_dir']

try:
    subprocess.check_output(failing_command)
except:
    ret = subprocess.CalledProcessError.returncode  # <- this seems to be wrong
    if ret in (1, 2):
        print("the command failed")
    elif ret in (3, 4, …
Run Code Online (Sandbox Code Playgroud)

python subprocess python-3.x

22
推荐指数
2
解决办法
5万
查看次数

如何在Python中从子进程获取返回代码和输出?

在为Android Debug Bridge(ADB)开发python包装器库时,我正在使用进程在shell中执行adb命令.这是简化的例子:

import subprocess

...

def exec_adb_command(adb_command):
    return = subprocess.call(adb_command)
Run Code Online (Sandbox Code Playgroud)

如果命令执行propery exec_adb_command返回0即可.

但是一些adb命令不仅会返回"0"或"1",还会生成一些我想要捕获的输出.adb设备例如:

D:\git\adb-lib\test>adb devices
List of devices attached
07eeb4bb        device
Run Code Online (Sandbox Code Playgroud)

我已经为此目的尝试了subprocess.check_output(),它确实返回输出但不返回代码("0"或"1").

理想情况下,我想得到一个元组,其中t [0]是返回码,t [1]是实际输出.

我是否遗漏了子流程模块中已经允许获得此类结果的内容?

谢谢!

python subprocess adb

22
推荐指数
1
解决办法
2万
查看次数

'builtin_function_or_method' 列出

我理解 Python 是一种动态语言的事实,但下面的代码让我很困扰。

我有下面的简单程序,它有一些帮助函数来包装命令执行。

EventLoaderToVerticaHelper 是一个有两种方法的辅助类,所以当我调用“get_filenames_from_hdfs_with_filter”时,它应该抛出一个错误或返回一个字符串列表。

class EventLoaderToVerticaHelper:
    def __init__(self):
        pass

    @staticmethod
    def execute_command(cmd):
        p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        error_lines = p.stderr.readlines()
        if len(error_lines) > 0:
            raise error_lines
        return p.stdout.readlines

    @staticmethod
    def get_filenames_from_hdfs_with_filter(folder, filetype):
        cmd = "hdfs dfs -ls {0}/*.{1} |  awk '{print $8}'".replace("{0}", folder).replace("{1}", filetype)
        return EventLoaderToVerticaHelper.execute_command(cmd) 
Run Code Online (Sandbox Code Playgroud)

下面的代码使用了上面的助手,

from src.EventLoaderToVerticaHelper import EventLoaderToVerticaHelper
if __name__ == '__main__':
     filelist = EventLoaderToVerticaHelper.get_filenames_from_hdfs_with_filter("/user/cloudera/testfiles", "csv")
     for s in filelist:
        print s
Run Code Online (Sandbox Code Playgroud)

当我运行上述程序时,出现以下错误。我确定返回类型如果 List[Str]

Traceback (most recent call last):
  File "/home/cloudera/PycharmProjects/vertical-event-loader/src/EventLoaderToVertica.py", line 29, in …
Run Code Online (Sandbox Code Playgroud)

python python-2.7 python-3.x

1
推荐指数
1
解决办法
1592
查看次数

标签 统计

python ×5

subprocess ×3

python-3.x ×2

adb ×1

command-line ×1

pipe ×1

python-2.7 ×1