SilliS
SilliS

Reputation: 175

pass parameter into subprocess.Popen arguments

I am new to Python and have some needs to write a script.

I have a parameter result and I need to pass it to argument in a subprocess.Popen

tried to do like this

proc = subprocess.Popen(['sed', '-i', '/x/c\y = 'result'', 
                         '/home/test/1.txt'], stdout=subprocess.PIPE) 

but it gives a syntax error. If I go like this:

proc = subprocess.Popen(['sed', '-i', '/x/c\y = ', result, 
                         '/home/test/1.txt'], stdout=subprocess.PIPE)

sed wrongly implements the result, not like the ending of /x/c\y = but like a separate value

Upvotes: 0

Views: 412

Answers (2)

vks
vks

Reputation: 67968

proc = subprocess.Popen("sed -i /x/c\y = "+result+" /home/test/1.txt",shell=True,stdout=subprocess.PIPE)

You can try with shell=True option as well if you are using sed from its default location.

Upvotes: 1

Ashoka Lella
Ashoka Lella

Reputation: 6729

You'll have to concatenate strings using the + operator

proc = subprocess.Popen(['sed', '-i', '/x/c\y = 'result'', ...\
                                                ^      ^

to

proc = subprocess.Popen(['sed', '-i', '/x/c\y = '+result+'', ...\

Upvotes: 1

Related Questions