I am trying to use a python script to call an external command a number of times, each time with a different variable.
I see there are recommendations to use subprocess.call to do this, however all the documentation I have found doesn’t explain how to pass this function variables (from a list for example).
I have tried a number of different formats, and I think a substitution would be the best, but it seems like the function wont accept the variable.
The code I am trying to get working is:
#!/usr/bin/python
domain_list = ["google.com", "google.ie"]
from subprocess import call
for x in domain_list:
print x
cmd_list = ['dig', '+short', '%s'] %x
call(cmd_list, shell=True)
This is failing with:
Traceback (most recent call last):
File "./dnscheck.py", line 16, in <module>
cmd_list = ['dig', '+short', '%s'] %arg
TypeError: unsupported operand type(s) for %: 'list' and 'str'
Really I want to be able to pass a bunch of different domains (defined in a list) to dig and receive the IP’s of those domains.
I’ve just started with python so sorry if this is very basic!
You are trying to use the modulo operator on a list, I presume you wanted to do string formatting on the last item, as the percentage symbol is overloaded to do formatting with strings, but as you just want to insert the item without any formatting, you can just give it directly:
If you did want to use formatting, you would need to do it on the string:
Note however, that this is pointless given you are not doing any formatting.