質問

I'm trying to call some shell program that use the environment and I'm trying to figure out the correct way. I boiled down to this snippet:

import subprocess as sp

p = sp.Popen(["echo","Hello","$FOO"], env = {"FOO":"42"}, stdout = sp.PIPE, shell = True)
p.wait()
print(p.communicate())

When "shell" is set to True it prints "('\n', None)" and when it's set to False it prints "('Hello $FOO\n', None)", but I was expecting it to print something along the lines of "('Hello 43\n', None)". What I'm doing wrong and is there a better way to do it?

役に立ちましたか?

解決

Try this:

p = sp.Popen("echo Hello $FOO", env={"FOO":"42"}, shell=True)

The output should be "Hello 42"

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top