- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我已经弄清楚如何使用 call() 让我的 python 脚本运行命令:
import subprocess
mycommandline = ['lumberjack', '-sleep all night', '-work all day']
subprocess.call(mycommandline)
这可行,但有一个问题,如果用户的命令路径中没有伐木 worker 怎么办?如果将伐木 worker 放在与 python 脚本相同的目录中,它会起作用,但是脚本如何知道它应该寻找伐木 worker ?我想如果有一个 command-not-found 错误,那么 lumberjack 就不会在命令路径中,脚本可以尝试找出它的目录是什么并在那里寻找 lumberjack,最后警告用户将 lumberjack 复制到其中之一如果在任何一个地方都没有找到那两个地方。如何找出错误消息是什么?我读到 check_call() 可以返回错误消息和有关 returncode 属性的内容。我找不到有关如何使用 check_call() 和 returncode、消息是什么或如何判断消息是否为 command-not-found 的示例。
我的做法是否正确?
最佳答案
一个简单的片段:
try:
subprocess.check_call(['executable'])
except subprocess.CalledProcessError:
pass # handle errors in the called executable
except OSError:
pass # executable not found
关于Python, subprocess, call(), check_call 和 returncode 来查找命令是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14106720/
亲们, 我正在尝试从此代码调用可执行文件,但由于某种原因它无法正常工作,(python 2.6) subprocess.check_call( ['fpm', '-s', 'dir', '-t',
是否可以得到下面的check_call过程: logPath="log.txt" with open(logPath,"w") as log: subprocess.check_ca
我想用 subprocess.check_call(cmd) 与 stdin argument .到目前为止,我发现的大多数教程都建议直接使用 Popen(例如 here ),但如果 cmd 出错,我
我尝试使用 subprocess 使用 'wc -l' 命令打印行数。它输出文件是空的,而它不是。 import os import subprocess import tempfile import
我试图在 python 脚本中使用 subprocess.ckeck_call 一次性运行多个命令,但不起作用。 >>> subprocess.check_call("testdriver ssh s
在运行 shell 脚本时,如何在 python 的子进程调用中传递多个参数? import subprocess subprocess.check_call('./test_bash.sh '+ar
这个问题在这里已经有了答案: How to redirect output with subprocess in Python? (6 个答案) 关闭 5 年前。 我正在尝试从这个命令中获取输出:“
我有如下命令 subprocess.check_call(["C:\\Program Files\\operation.exe", "execute", "-af", "createrecord.xm
我有一个 python 脚本,它使用 subprocess.check_call 启动 Wine(Linux 上的 Windows 模拟器),然后 wine 启动 Z:\\Program Files
我将 subprocess.check_call 与 rsync 结合使用。 我需要为 rsync 使用来自包含多个空格分隔值的字符串的参数,但是因为字符串是单个对象,它在 subprocess.ch
我的 python 程序正在使用 check_call 调用 Windows 上的脚本: import subprocess subprocess.check_call(['my_script.bat
我怎样才能让 subprocess.check_call 给我一个命令的原始二进制输出,它似乎在某处编码不正确。 详细信息: 我有一个返回如下文本的命令: some output text “quot
我在 Ubuntu 12 上使用 Python 2.7 运行这些片段。 import subprocess args = ['rsync', '--rsh="ssh"', '/tmp/a/', '12
我的 python 脚本(python 3.4.3)通过子进程调用 bash 脚本: import subprocess as sp res = sp.check_output("bashscript
我正在尝试使用 subprocess.check_call 通过 Python 运行 Rscript。 Rscript 非常简单,它只是检查 Rpackage 是否存在,如果不存在则安装它。 loca
我有一个路径“D:\Torres\Gas_Entrapment\new_calculations\command_script_load\Es_cteS_cte\w_load”,其中存储了 9(九)个
我想调用一个子进程来备份mysql数据库。在终端中运行良好的命令行(并创建了一个名为 mydatabase.sql 的文件)是: mysqldump -uroot -ppassword --a
我在使用 python subprocess.check_call() 函数时遇到了一个非常奇怪的错误。这里有两个测试应该都因权限问题而失败,但第一个只返回“用法”(“意外行为”): # Test #
N = 50000 with open('input', 'w') as f: for i in range(N): f.write(str(i) + '\n') run_co
我在调用 shell 脚本时遇到以下错误,如何使用 check_call 或通过任何其他 python 函数调用 shellscript? 导入操作系统 从子进程导入 check_call,Popen
我是一名优秀的程序员,十分优秀!