Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python subprocess.run shell kwarg行为_Python_Shell_Subprocess - Fatal编程技术网

Python subprocess.run shell kwarg行为

Python subprocess.run shell kwarg行为,python,shell,subprocess,Python,Shell,Subprocess,关于shell关键字参数有很多问题。但我还是不太明白,特别是如果我们使用序列参数而不是字符串 我的理解是,如果shell=False,则子流程模块将在args[0]中运行可执行文件,并将其余部分作为参数传递给可执行文件。但是如果我们使用shell=True运行它,它将像中的“sh-c{}”一样运行。format(format_-escaping(args)) 但为什么会发生这种情况 #在OSX中运行 subprocess.run([“touch”,“12;touch 34”])#成功生成文件'1

关于
shell
关键字参数有很多问题。但我还是不太明白,特别是如果我们使用序列参数而不是字符串

我的理解是,如果
shell=False
,则
子流程
模块将在
args[0]
中运行可执行文件,并将其余部分作为参数传递给可执行文件。但是如果我们使用
shell=True
运行它,它将像
中的“sh-c{}”一样运行。format(format_-escaping(args))

但为什么会发生这种情况

#在OSX中运行
subprocess.run([“touch”,“12;touch 34”])#成功生成文件'12;触摸34'
子进程运行([“touch”,“56;touch 78”],shell=True)#不工作:
#用法:
#触摸[-A[-][[hh]mm]SS][acfhm][r文件][t[[CC]YY]MMDDhhmm[.SS]]文件。。。
#CompletedProcess(args=['touch','123;touch 456'],returncode=1)

subprocess.run([“touch”,“56;touch 79”],shell=True)

我认为使用
shell=True
时,supbrocess只会首先运行第一个
参数,即
touch
,因此您可以通过命令成为帮助表,改为这样尝试:

subprocess.run("touch 56; touch 78", shell=True)

我认为,使用
shell=True
时,Supbroess首先运行的是第一个
参数,即
touch
,因此您可以通过该命令成为help mesage,请改为这样尝试:

subprocess.run("touch 56; touch 78", shell=True)

使用
shell=True
传递列表时的行为取决于平台。在类Unix平台上,Python只需传入列表的第一个参数。在Windows上,它碰巧可以工作,但可能不应该工作。

传入带有
shell=True
的列表时的行为取决于平台。在类Unix平台上,Python只需传入列表的第一个参数。在Windows上,它正好工作,但可能不应该工作。

我明白了,那么其余的参数呢?e、 g.当我运行
子流程时。运行([“touch”,“touch”])
?您应该将所有参数放在一个位置,您的第一个示例也会给出
12;触摸34
,这样就可以作为参数来第一次触摸,而不是像使用shell=TrueSo时那样的命令。run(;”。join(commands),shell=True)
相当于
subprocess.run(commands,shell=True)
?我明白了,那么其余的参数会怎么样?e、 g.当我运行
子流程时。运行([“touch”,“touch”])
?您应该将所有参数放在一个位置,您的第一个示例也会给出
12;触摸34
以使第一次触摸的参数与使用shell=TrueSo时的命令不同。run(;”。join(commands),shell=True)
相当于
subprocess.run(commands,shell=True)