【问题标题】:Strange subprocess behavior奇怪的子进程行为
【发布时间】:2017-07-18 10:33:02
【问题描述】:
subprocess.call(["find", ".", "-exec", "sh", "-c", "echo testFirst", ";"])
subprocess.call(["find", ".", "-exec", "sh", "-c", "echo testSecond", ";"], shell=True)
subprocess.call(["find . -exec sh -c 'echo testThird' \\;"], shell=True)

subprocess.call(["find", ".", "-exec", "sh", "-c", "touch testFirst", ";"])
subprocess.call(["find", ".", "-exec", "sh", "-c", "touch testSecond", ";"], shell=True)
subprocess.call(["find . -exec sh -c 'touch testThird' \\;"], shell=True)

以下输出:

testFirst
testFirst
testFirst
.
./test.py
./data
testThird
testThird
testThird
.
./test.py
./testFirst
./data

并且只创建testFirsttestThird 文件。

行为的解释是什么?

我假设输出为testFirsttestSecondtestThird 以及正在创建的三个文件。

【问题讨论】:

  • 为什么需要前 4 个参数?
  • 这不是我的实际代码,我只是缩小范围来演示问题。

标签: python subprocess


【解决方案1】:

https://stackoverflow.com/a/10661488/1663462

当你传递 shell=True 时,Popen 需要一个字符串参数,而不是 一个列表。

...........

【讨论】:

    猜你喜欢
    • 2021-07-03
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 2014-04-15
    • 2011-03-18
    相关资源
    最近更新 更多