Reputation:
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
Upvotes: 8
Views: 38806
Reputation: 83032
There is ANOTHER problem in the OP's code:
z = ('0.')
then later for k in z:
The parentheses in the first statement will be ignored, leading to the second statement binding k
first to '0'
and then '.'
... looks like z = ('0.', )
was intended.
Upvotes: 2
Reputation: 212158
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",)
Upvotes: 11
Reputation: 60644
string objects can only be concatenated with other strings. Python is a strongly-typed language. It will not coerce types for you.
you can do:
'a' + '1'
but not:
'a' + 1
in your case, you are trying to concat a string and a list. this won't work. you can append the item to the list though, if that is your desired result:
my_list.append('a')
Upvotes: 4