<bdo id='8SCgy'></bdo><ul id='8SCgy'></ul>

  1. <small id='8SCgy'></small><noframes id='8SCgy'>

  2. <legend id='8SCgy'><style id='8SCgy'><dir id='8SCgy'><q id='8SCgy'></q></dir></style></legend>

      <tfoot id='8SCgy'></tfoot>

    1. <i id='8SCgy'><tr id='8SCgy'><dt id='8SCgy'><q id='8SCgy'><span id='8SCgy'><b id='8SCgy'><form id='8SCgy'><ins id='8SCgy'></ins><ul id='8SCgy'></ul><sub id='8SCgy'></sub></form><legend id='8SCgy'></legend><bdo id='8SCgy'><pre id='8SCgy'><center id='8SCgy'></center></pre></bdo></b><th id='8SCgy'></th></span></q></dt></tr></i><div id='8SCgy'><tfoot id='8SCgy'></tfoot><dl id='8SCgy'><fieldset id='8SCgy'></fieldset></dl></div>
    2. 使用带有列表的 shell=True 时忽略 subprocess.call() 参数

      subprocess.call() arguments ignored when using shell=True w/ list(使用带有列表的 shell=True 时忽略 subprocess.call() 参数)

          1. <small id='f68W7'></small><noframes id='f68W7'>

            • <bdo id='f68W7'></bdo><ul id='f68W7'></ul>

              <i id='f68W7'><tr id='f68W7'><dt id='f68W7'><q id='f68W7'><span id='f68W7'><b id='f68W7'><form id='f68W7'><ins id='f68W7'></ins><ul id='f68W7'></ul><sub id='f68W7'></sub></form><legend id='f68W7'></legend><bdo id='f68W7'><pre id='f68W7'><center id='f68W7'></center></pre></bdo></b><th id='f68W7'></th></span></q></dt></tr></i><div id='f68W7'><tfoot id='f68W7'></tfoot><dl id='f68W7'><fieldset id='f68W7'></fieldset></dl></div>
              <tfoot id='f68W7'></tfoot>

              1. <legend id='f68W7'><style id='f68W7'><dir id='f68W7'><q id='f68W7'></q></dir></style></legend>
                  <tbody id='f68W7'></tbody>
                本文介绍了使用带有列表的 shell=True 时忽略 subprocess.call() 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试让 python 的 subprocess.call 方法按照 python 文档中的建议通过列表(由一系列字符串组成)接受一些 args 命令.为了在将它放入我的实际脚本之前探索这种行为,我打开了 IPython,运行了一些涉及 shell 设置和 args 命令的不同组合的命令,并得到了以下行为:

                I am trying to get python's subprocess.call method to accept some args commands through a list (consisting of a sequence of strings) as advised in the python documentation. To explore this behavior before putting it into my actual script, I opened up IPython, ran some commands involving different combinations of shell settings and args commands and got the following behavior:

                In [3]: subprocess.call(['ls', '-%sl' %'a'])
                total 320
                drwxr-xr-x  20 Kohaugustine  staff   680 Oct 15 16:55 .
                drwxr-xr-x   5 Kohaugustine  staff   170 Sep 12 17:16 ..
                -rwxr-xr-x   1 Kohaugustine  staff  8544 Oct 15 16:55 a.out
                -rwxr-xr-x   1 Kohaugustine  staff  8544 Oct  3 10:28 ex1-6
                -rw-r--r--@  1 Kohaugustine  staff   204 Oct  3 10:28 ex1-6.c
                -rwxr-xr-x   1 Kohaugustine  staff  8496 Oct  3 10:15 ex1-7
                -rw-r--r--@  1 Kohaugustine  staff    71 Oct  3 10:15 ex1-7.c
                -rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 12 16:22 hello
                -rw-r--r--@  1 Kohaugustine  staff    58 Sep 12 16:27 hello.c
                -rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 12 16:24 hello.o
                -rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 12 16:24 hello_1.o
                -rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 12 16:27 hello_2.o
                -rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 12 16:27 hello_3.o
                -rwxr-xr-x   1 Kohaugustine  staff  8544 Oct 15 16:55 lesson_1-5
                -rw-r--r--@  1 Kohaugustine  staff   185 Sep 28 10:35 lesson_1-5.c
                -rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 21 10:06 temperature.o
                -rw-r--r--@  1 Kohaugustine  staff   406 Sep 21 09:54 temperature_ex1-3.c
                -rw-r--r--@  1 Kohaugustine  staff   582 Sep 21 10:06 temperature_ex1-4.c
                -rw-r--r--@  1 Kohaugustine  staff   178 Sep 23 17:21 temperature_ex1-5.c
                -rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 23 17:21 temperature_ex1-5.o
                Out[3]: 0
                
                In [4]: subprocess.call(['ls', '-%sl' %'a'], shell=True)
                a.out           ex1-7           hello.c         hello_2.o       lesson_1-5.c            temperature_ex1-4.c
                ex1-6           ex1-7.c         hello.o         hello_3.o       temperature.o           temperature_ex1-5.c
                ex1-6.c         hello           hello_1.o       lesson_1-5      temperature_ex1-3.c     temperature_ex1-5.o
                Out[4]: 0
                
                In [6]: subprocess.call(['ls', '-al'])    
                total 320
                drwxr-xr-x  20 Kohaugustine  staff   680 Oct 15 16:55 .
                drwxr-xr-x   5 Kohaugustine  staff   170 Sep 12 17:16 ..
                -rwxr-xr-x   1 Kohaugustine  staff  8544 Oct 15 16:55 a.out
                -rwxr-xr-x   1 Kohaugustine  staff  8544 Oct  3 10:28 ex1-6
                -rw-r--r--@  1 Kohaugustine  staff   204 Oct  3 10:28 ex1-6.c
                -rwxr-xr-x   1 Kohaugustine  staff  8496 Oct  3 10:15 ex1-7
                -rw-r--r--@  1 Kohaugustine  staff    71 Oct  3 10:15 ex1-7.c
                -rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 12 16:22 hello
                -rw-r--r--@  1 Kohaugustine  staff    58 Sep 12 16:27 hello.c
                -rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 12 16:24 hello.o
                -rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 12 16:24 hello_1.o
                -rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 12 16:27 hello_2.o
                -rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 12 16:27 hello_3.o
                -rwxr-xr-x   1 Kohaugustine  staff  8544 Oct 15 16:55 lesson_1-5
                -rw-r--r--@  1 Kohaugustine  staff   185 Sep 28 10:35 lesson_1-5.c
                -rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 21 10:06 temperature.o
                -rw-r--r--@  1 Kohaugustine  staff   406 Sep 21 09:54 temperature_ex1-3.c
                -rw-r--r--@  1 Kohaugustine  staff   582 Sep 21 10:06 temperature_ex1-4.c
                -rw-r--r--@  1 Kohaugustine  staff   178 Sep 23 17:21 temperature_ex1-5.c
                -rwxr-xr-x   1 Kohaugustine  staff  8496 Sep 23 17:21 temperature_ex1-5.o
                Out[6]: 0
                
                In [7]: subprocess.call(['ls', '-al'], shell = True)
                a.out           ex1-7           hello.c         hello_2.o       lesson_1-5.c            temperature_ex1-4.c
                ex1-6           ex1-7.c         hello.o         hello_3.o       temperature.o           temperature_ex1-5.c
                ex1-6.c         hello           hello_1.o       lesson_1-5      temperature_ex1-3.c     temperature_ex1-5.o
                Out[7]: 0
                

                似乎每当 shell=True 时,输出似乎与以下内容相同:

                It seems like whenever shell=True, the output seems to be the same as:

                In [9]: subprocess.call(['ls'])
                a.out           ex1-7           hello.c         hello_2.o       lesson_1-5.c            temperature_ex1-4.c
                ex1-6           ex1-7.c         hello.o         hello_3.o       temperature.o           temperature_ex1-5.c
                ex1-6.c         hello           hello_1.o       lesson_1-5      temperature_ex1-3.c     temperature_ex1-5.o
                Out[9]: 0
                

                我很困惑;当我设置 shell=True 时,'-a' 选项发生了什么?壳没读吗?我已经阅读了文档,它说当 shell=True 时,这意味着我指定的命令将通过 shell 执行,所以这应该意味着 ls -a 被馈送到 shell 并由 shell 执行.那么为什么会出现 [4] 和 [7] 中的行为?pydocs 也没有直接解释它(尽管它确实说明了当我们设置 shell=False 时 subpprocess 不会做的事情);当我们让 shell=False 时是什么意思?是否在 OS 中生成了一个新进程,而没有 shell 实际控制它?

                I'm puzzled; what happened to the '-a' option when I set shell=True? Didn't the shell read it? I've read the Docs and that it says that when shell=True, it means that my specified command will be executed through the shell, so it should mean that ls -a was fed to the shell and acted upon by the shell. Then why the behavior in [4] and [7] ? Also the pydocs doesn't explain it directly (although it does say what subpprocess will NOT DO when we set shell=False); what does it mean when we let shell=False? Is a new process spawned in the OS without having the shell actually control it?

                另外,如果我在 [3] 和 [4] 中使用格式字符串可能看起来很尴尬,这是因为在我将使用 subprocess.call 的实际脚本中,我将不得不依赖在这些格式字符串上替换为适当的命令选项.我无法对某些命令行选项进行硬编码.对 args 使用纯字符串也是不可能的,因为在我的脚本中将有一个方法必须对命令执行列表操作.我不知道是否有更好的方法来解决这个问题,所以如果有人可以提出不同的建议,如果真的会有所帮助.

                Also, in case it might seem really awkward that I'm using a format string in [3] and [4], its because in my actual script where I'll be using subprocess.call, I will have to rely on these format strings to substitute in the appropriate command options. I cannot hardcode some of the command line options. Using a pure string for args is out of the question too because in my script there will be a method that has to do list operations on the commands. I don't know if there might be a better way to go about this though, so if will really help if anyone can suggest something different.

                非常感谢!

                推荐答案

                shell为True时,将第一个参数追加到["/bin/sh", "-c"].如果该参数是一个列表,则结果列表是

                When shell is True, the first argument is appended to ["/bin/sh", "-c"]. If that argument is a list, the resulting list is

                ["/bin/sh", "-c", "ls", "-al"]
                

                也就是说,只有 ls,而不是 ls -al 用作 -c 选项的参数.-al 用作 shell 本身的第一个参数,而不是 ls.

                That is, only ls, not ls -al is used as the argument to the -c option. -al is used as the first argument the shell itself, not ls.

                当使用 shell=True 时,您通常只想传递一个字符串,然后让 shell 根据 shell 的正常分词规则对其进行拆分.

                When using shell=True, you generally just want to pass a single string and let the shell split it according the shell's normal word-splitting rules.

                # Produces ["/bin/sh", "-c", "ls -al"]
                subprocess.call("ls -al", shell=True)
                

                在您的情况下,您根本不需要使用 shell=True.

                In your case, it doesn't see like you need to use shell=True at all.

                这篇关于使用带有列表的 shell=True 时忽略 subprocess.call() 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)
                  <bdo id='oPAAW'></bdo><ul id='oPAAW'></ul>

                        <tbody id='oPAAW'></tbody>
                      <tfoot id='oPAAW'></tfoot>
                    1. <small id='oPAAW'></small><noframes id='oPAAW'>

                        <i id='oPAAW'><tr id='oPAAW'><dt id='oPAAW'><q id='oPAAW'><span id='oPAAW'><b id='oPAAW'><form id='oPAAW'><ins id='oPAAW'></ins><ul id='oPAAW'></ul><sub id='oPAAW'></sub></form><legend id='oPAAW'></legend><bdo id='oPAAW'><pre id='oPAAW'><center id='oPAAW'></center></pre></bdo></b><th id='oPAAW'></th></span></q></dt></tr></i><div id='oPAAW'><tfoot id='oPAAW'></tfoot><dl id='oPAAW'><fieldset id='oPAAW'></fieldset></dl></div>
                        <legend id='oPAAW'><style id='oPAAW'><dir id='oPAAW'><q id='oPAAW'></q></dir></style></legend>