- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以最初的问题是:
# works(1)
subprocess.check_call(["ls", "-l"])
#works(2), but "-l" argument not passed to called process
subprocess.check_call(["ls", "-l"], shell=True)
#works(3) again
subprocess.check_call("ls -l", shell=True)
#Exception(4): "bufsize must be an integer"
subprocess.check_call("ls", "-l", shell=True)
如果查看文档( https://docs.python.org/2/library/subprocess.html ),subprocess.check_call 具有这样的签名:subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
我知道Python中的函数是这样的:def f(*args, **kwargs)
,但是, *
是什么意思?它与*args
相同吗?
无论如何,之后在文档中注意到:
The full function signature is the same as that of the Popen constructor
Popen 构造函数签名是:
Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)
所以很清楚为什么(4)失败了,但为什么(2)却以错误的方式工作?如何阅读这个奇怪的符号:args, *, stdin=None
?
更新
我找到了为什么 (2) 不起作用。谢谢回复我明白了从语法的角度来看这是正确的。所以我运行 strace,并看到 python 以错误的方式将参数传递给“/bin/sh”。已知错误:http://bugs.python.org/issue6689并被拒绝。这真是令人悲伤。
最佳答案
(2) 是正确的。列表["ls", "-l"]
作为args
传递给方法(Popen.__init__
)。
*
表示后面的参数是 Keyword-only arguments 。它是在 Python 3.x 中引入的。但正如您在源代码中看到的那样,实际签名不使用仅关键字参数。因此,您可以传递 args
以外的参数的位置参数。
关于python + subprocess.check_call : how to call functions in python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25826098/
亲们, 我正在尝试从此代码调用可执行文件,但由于某种原因它无法正常工作,(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
我是一名优秀的程序员,十分优秀!