I have a small python snippet that calls a larger program (I did not write the larger one).
call(['function1', file1, file2, 'data.labels=abc, xyz'])
The above works.
input ='abc, xyz'
Now I want to input "abc, xyz" as a variable holding this value
call(['function1', file1, file2, 'data.labels=input'])
but it does not work.
How can I pass a variable value into variable data.labels within call subprocess.
3 Answers 3
call(['function1', file1, file2, 'data.labels=%s' % input])
Sign up to request clarification or add additional context in comments.
1 Comment
jfs
'data.labels=' + input seems like a more natural code.Or
call(['function1', file1, file2, 'data.labels=' + input)
If for some reason, input is not a string.
call(['function1', file1, file2, 'data.labels=' + str(input) )
answered Sep 9, 2014 at 20:37
Robert Jacobs
3,3801 gold badge22 silver badges31 bronze badges
Comments
Another way to do the same:
call(['function1', file1, file2, 'data.labels={0}'.format(input)])
answered Sep 9, 2014 at 20:18
Raydel Miranda
14.4k3 gold badges48 silver badges66 bronze badges
Comments
lang-py
'abc, xyz'and somehow get the string'data.labels=abc, xyz'"? The answer isn't really specific tocallat all, just ordinary string manipulation.subprocess.call()on Windows whereCreateProcess()is used to start the child process that accepts a single string as a command, seesubprocess.list2cmdline(). In other words, you can't pass arbitrary unescaped values on Windows becauselist2cmdline()doesn't cover all possible cases.