shell调用python函数 参数 返回_python – 使用shell时返回subprocess.call()参数...

本文探讨了在Python中使用subprocess.call时,通过列表传递参数与设置shell=True的影响。实验显示,当shell=True时,命令的执行方式与预期不符。作者通过示例展示了ls命令的不同执行结果,并对可能的原因进行了疑惑。文章末尾,作者提出了关于如何优雅地处理动态命令参数的疑问。
摘要由CSDN通过智能技术生成

参见英文答案 > subprocess.call using string vs using list????????????????????????????????????1个

我试图让python的subprocess.call方法通过列表(由一系列字符串组成)接受一些args命令,如python文档中所建议的那样.为了在将它放入我的实际脚本之前探索这种行为,我打开了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时子进程不会做什么);当我们让shell = False时它意味着什么?是否在操作系统中生成了一个新进程而没有shell实际控制它?

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

非常感谢你!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值