如何获取结果子进程命令字符串 [英] How to get resulting subprocess command string
问题描述
我有 Python 子进程调用,它们被格式化为一系列参数(如 subprocess.Popen(['ls','-l'])
而不是单个字符串(即 subprocess.Popen('ls -l')
).
像我一样使用序列参数时,有没有办法获取发送到 shell 的结果字符串(用于调试目的)?
一种简单的方法是自己将所有论点结合在一起.但我怀疑这在所有情况下都与 subprocess 所做的相同,因为使用序列的主要原因是 '允许 [s] 模块处理任何必需的参数转义和引用'.
如评论中所述,subprocess
附带(未在文档页面中记录)list2cmdline
将参数列表转换为单个字符串.根据源文档,list2cmdline
主要用于 Windows:
在 Windows 上:Popen 类使用 CreateProcess() 来执行子进程程序,它对字符串进行操作.如果 args 是一个序列,它将是使用 list2cmdline 方法转换为字符串.请注意并非所有 MS Windows 应用程序对命令行的解释都相同方式:list2cmdline 是为使用相同的应用程序而设计的规则作为 MS C 运行时.
尽管如此,它在其他操作系统上非常有用.
编辑
如果您需要反向操作(ie,将命令行拆分为正确标记化的参数列表),您将需要使用 shlex.split
函数,如 subprocess
文档中所示一>.
I have Python subprocess calls which are formatted as a sequence of arguments (like subprocess.Popen(['ls','-l'])
instead of a single string (i.e. subprocess.Popen('ls -l')
).
When using sequenced arguments like I did, is there a way to get the resulting string that is sent to the shell (for debugging purposes)?
One simple approach would be to join all arguments together myself. But I doubt this would in all cases be identical to what subprocess does, since the main reason for using a sequence is to 'allow[s] the module to take care of any required escaping and quoting of arguments'.
As mentioned in a comment, subprocess
comes with (not documented in the docs pages) list2cmdline
that transforms a list of arguments into a single string.
According to the source doc, list2cmdline
is used mostly on Windows:
On Windows: the Popen class uses CreateProcess() to execute the child program, which operates on strings. If args is a sequence, it will be converted to a string using the list2cmdline method. Please note that not all MS Windows applications interpret the command line the same way: The list2cmdline is designed for applications using the same rules as the MS C runtime.
Nevertheless, it's quite usable on other OSes.
EDIT
Should you need the reverse operation (ie, splitting a command line into a list of properly tokenized arguments), you'll want to use the shlex.split
function, as illustrated in the doc of subprocess
.
>>> help(subprocess.list2cmdline)
Help on function list2cmdline in module subprocess:
list2cmdline(seq)
Translate a sequence of arguments into a command line
string, using the same rules as the MS C runtime:
1) Arguments are delimited by white space, which is either a
space or a tab.
2) A string surrounded by double quotation marks is
interpreted as a single argument, regardless of white space
contained within. A quoted string can be embedded in an
argument.
3) A double quotation mark preceded by a backslash is
interpreted as a literal double quotation mark.
4) Backslashes are interpreted literally, unless they
immediately precede a double quotation mark.
5) If backslashes immediately precede a double quotation mark,
every pair of backslashes is interpreted as a literal
backslash. If the number of backslashes is odd, the last
backslash escapes the next double quotation mark as
described in rule 3.
这篇关于如何获取结果子进程命令字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!