Skip to main content
 首页 » 编程设计

python之使用 shell=True w/list 时忽略 subprocess.call() 参数

2025年05月04日10zdz8207

<分区>

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

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 时,输出似乎与:

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 没有读取它吗?我读过文档,它说当 shell=True 时,这意味着我指定的命令将通过 shell 执行,所以这应该意味着 ls -a 被馈送到 shell 并由 shell 执行。那么为什么 [4] 和 [7] 中的行为呢? pydocs 也没有直接解释它(尽管它确实说明了当我们设置 shell=False 时 subpprocess 不会做什么);当我们让 shell=False 时,这意味着什么?操作系统中是否产生了一个新进程,而 shell 并未实际控制它?

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

非常感谢!