0

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.

user3456863
  • 45
  • 2
  • 8
  • A more specific question might be, "how do I take the string `'abc, xyz'` and somehow get the string `'data.labels=abc, xyz'`"? The answer isn't really specific to `call` at all, just ordinary string manipulation. – Kevin Sep 09 '14 at 20:16
  • @Kevin: It is specific to `subprocess.call()` on Windows where `CreateProcess()` is used to start the child process that accepts a single string as a command, see `subprocess.list2cmdline()`. In other words, you can't pass arbitrary unescaped values on Windows because `list2cmdline()` doesn't cover all possible cases. – jfs Sep 09 '14 at 22:29

3 Answers3

2
call(['function1', file1,  file2,  'data.labels=%s' % input])
chepner
  • 497,756
  • 71
  • 530
  • 681
Jonas K
  • 4,215
  • 2
  • 24
  • 25
  • [`'data.labels=' + input`](http://stackoverflow.com/a/25753183/4279) seems like a more natural code. – jfs Sep 09 '14 at 22:31
1

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) )
Robert Jacobs
  • 3,266
  • 1
  • 20
  • 30
0

Another way to do the same:

call(['function1', file1,  file2,  'data.labels={0}'.format(input)])
Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60