【发布时间】:2019-08-29 07:00:04
【问题描述】:
关于shell 关键字参数有很多问题。但我仍然不明白,尤其是如果我们使用序列参数而不是字符串。
我的理解是,如果shell=False,subprocess 模块将在args[0] 中运行可执行文件,并将其余部分作为参数传递给可执行文件。但是如果我们用shell=True 运行它,它就会像"sh -c {}".format(format_escaping(args)) 这样运行。
但是为什么会这样呢?
# Ran in OSX
subprocess.run(["touch", "12; touch 34"]) # successfuly make the file '12; touch 34'
subprocess.run(["touch", "56; touch 78"], shell=True) # does not work:
# usage:
# touch [-A [-][[hh]mm]SS] [-acfhm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...
# CompletedProcess(args=['touch', '123; touch 456'], returncode=1)
subprocess.run(["touch", "56; touch 79"], shell=True) 实际发生了什么?
【问题讨论】:
标签: python shell subprocess