- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 subprocess
模块和 check_output()
在我的 Python 脚本中创建一个虚拟 shell,它适用于返回零退出状态的命令,然而,对于那些不这样做的,它会返回一个异常,而不会打印在普通 shell 的输出中显示的错误。
例如,我希望某些东西能像这样工作:
>>> shell('cat non-existing-file')
cat: non-existing-file: No such file or directory
但是,这会发生:
>>> shell('cat non-existing-file')
CalledProcessError: Command 'cat non-existing-file' returned non-zero exit status 1 (file "/usr/lib/python2.7/subprocess.py", line 544, in check_output)
即使我可以使用 try
和 except
删除 Python 异常消息,我仍然想要 cat: non-existing-file: No such file 或者显示给用户的目录
。
我该怎么做?
shell()
:
def shell(command):
output = subprocess.check_output(command, shell=True)
finished = output.split('\n')
for line in finished:
print line
return
最佳答案
也许是这样的?
def shell(command):
try:
output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
except Exception, e:
output = str(e.output)
finished = output.split('\n')
for line in finished:
print line
return
关于python - 忽略 CalledProcessError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12015766/
我有一个脚本循环遍历几个文件路径,以便使用子进程执行一些其他 python 和 bash 脚本。 我知道这些脚本偶尔会失败,我想捕获异常并停止循环/退出调用脚本。这看起来应该很简单 - 但我很想让它发
我正在使用 subprocess 模块和 check_output() 在我的 Python 脚本中创建一个虚拟 shell,它适用于返回零退出状态的命令,然而,对于那些不这样做的,它会返回一个异常,
我希望有人可以帮助解决这个问题。我正在尝试以以下动画的形式从 Keras 中保存损失图。 但我一直面临以下错误,最终我无法保存动画: MovieWriter stderr: [h264_v4l2m2m
我已经通过 brew 安装了 Cmake 并查看了错误跟踪器和其他有这个问题的人,但我没有找到任何有我问题的人。这是输出中的错误: Linking CXX shared library /Users/
我一直在使用一个执行 bash 命令的简短 python 脚本。该程序运行了大约一个月,效果良好。最近,我尝试运行脚本并向其传递以下命令: my_launcher.py -c /path/to/con
我使用 python,我想创建一个数据库源 Controller ,如 liquibase 。我找到liquibase调用的python版本 pyquibase 但得到 subprocess.Call
我正在使用下面的代码获取 shell 命令的输出。 import subprocess exitcode, err, out = 0, None, None try: out = subpro
import subprocess cmd = "grep -r * | grep jquery" print cmd subprocess.check_output(cmd, shell=True)
继续 from my previous question我看到要获取我在 python 中通过 Popen 生成的进程的错误代码,我必须调用 wait() 或 communicate() (可用于访问
我正在使用 python 子进程模块中的 subprocess.check_output 来执行 ping 命令。这是我的做法: output = subprocess.check_output(["
我想在python(3)脚本中捕获shell命令的stdout流,同时能够检查shell命令的返回码是否返回一个错误(也就是说,如果它的返回码不是 0)。 subprocess.check_outpu
运行时出现以下错误 buildozer android debug deploy run (VirtualBox 上的 Ubuntu 64 14.04): Traceback (most recent
在asyncio模块中是否有一个与CalledProcessError类似的东西?来自 subprocess 模块的异常? 我本来希望有一个模拟,因为 asyncio 模块创建 TimeoutErro
>>> import subprocess >>> subprocess.check_output("smartctl -d ata -a /dev/sda", shell=True) "output
在运行 kivy 时,出现此错误。我已经安装了 ant。 subprocess.CalledProcessError: Command '['ant', 'debug']' returned non-
我正在尝试实现以下代码: import os os.environ.update({'MALLET_HOME':r'c:/mallet-2.0.8/'}) mallet_path = 'C:\\mal
当我在约 1600 万个文档的完整语料库上运行 Gensim LDAMallet 模型时,出现 CalledProcessError“非零退出状态 1”错误。有趣的是,如果我在包含约 160,000
以下是我们代码库中的代码片段 # global library function def check_call_noout(params, acceptable_exit_codes = (0,),
我想调用一个子进程来备份mysql数据库。在终端中运行良好的命令行(并创建了一个名为 mydatabase.sql 的文件)是: mysqldump -uroot -ppassword --a
这个问题已经有答案了: Subprocess check_output returned non-zero exit status 1 (2 个回答) 已关闭 5 年前。 我需要计算 python 脚
我是一名优秀的程序员,十分优秀!