如何获取结果子进程命令字符串 [英] How to get resulting subprocess command string

查看:26
本文介绍了如何获取结果子进程命令字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 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 文档中所示一>.

<预><代码>>>>帮助(subprocess.list2cmdline)模块子进程中函数 list2cmdline 的帮助:list2cmdline(seq)将参数序列转换为命令行字符串,使用与 MS C 运行时相同的规则:1) 参数由空格分隔,它是一个空格或制表符.2) 用双引号括起来的字符串是解释为单个参数,与空格无关内含.带引号的字符串可以嵌入到争论.3) 以反斜杠开头的双引号是解释为文字双引号.4) 反斜杠按字面解释,除非它们紧跟在双引号之前.5) 如果反斜杠紧跟在双引号之前,每对反斜杠都被解释为文字反斜杠.如果反斜杠的数量是奇数,最后一个反斜杠将下一个双引号转义为规则 3 中描述.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆