What does this error mean?
TypeError: cannot concatenate 'str' and 'list' objects
Here's part of the code:
for j in ('90.','52.62263.','26.5651.','10.8123.'):if j == '90.':z = ('0.')elif j == '52.62263.':z = ('0.', '72.', '144.', '216.', '288.')for k in z:exepath = os.path.join(exe file location here)exepath = '"' + os.path.normpath(exepath) + '"'cmd = [exepath + '-j' + str(j) + '-n' + str(z)]process=Popen('echo ' + cmd, shell=True, stderr=STDOUT )print process
I'm not sure you're aware that cmd
is a one-element list
, and not a string.
Changing that line to the below would construct a string, and the rest of your code will work:
# Just removing the square brackets
cmd = exepath + '-j' + str(j) + '-n' + str(z)
I assume you used brackets just to group the operations. That's not necessary if everything is on one line. If you wanted to break it up over two lines, you should use parentheses, not brackets:
# This returns a one-element list
cmd = [exepath + '-j' + str(j) + '-n' + str(z)]# This returns a string
cmd = (exepath + '-j' + str(j) + '-n' + str(z))
Anything between square brackets in python is always a list
. Expressions between parentheses are evaluated as normal, unless there is a comma in the expression, in which case the parentheses act as a tuple
constructor:
# This is a string
str = ("I'm a string")# This is a tuple
tup = ("I'm a string","me too")# This is also a (one-element) tuple
tup = ("I'm a string",)