geekcomputers
geekcomputers

Reputation: 119

TypeError: cannot concatenate 'str' and 'list' objects

I have a snippet of code which works:

p = subprocess.Popen('psftp servername'.split(),stdin=subprocess.PIPE, tdout=subprocess.PIPE, shell=True)   
p.stdin.write('lcd P:\\ORACLE_UNIX\\Development\n')    
p.stdin.write('get //opt//jboss//current//server//default//conf//DMS.properties\n')    
p.stdin.write('bye\n')    
p.stdout.close()    
p.stdin.close()

But when I have a variable set (as I will refer to the server in other parts):

devserv='servername'  
p = subprocess.Popen('psftp' +devserv.split(),stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
p.stdin.write('lcd P:\\ORACLE_UNIX\\Development\n')    
p.stdin.write('get //opt//jboss//current//server//default//conf//DMS.properties\n')    
p.stdin.write('bye\n')    
p.stdout.close()    
p.stdin.close()

...I always get TypeError: cannot concatenate 'str' and 'list' objects. Why?

Upvotes: 0

Views: 3048

Answers (2)

Shawn Chin
Shawn Chin

Reputation: 86854

devserver.split() returns a list, which is why you're seeing that error - you're trying to concatenate a string with a list.

You could try this instead:

p = subprocess.Popen(['psftp', devserver], stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)

.split() is no longer needed since you already have the cmd and args as seperate strings.

Upvotes: 1

MAK
MAK

Reputation: 26586

I think you need to replace

'psftp' +devserv.split()

with

('psftp' +devserv).split()

You need to build the string first and then call split on the completed string.

Upvotes: 1

Related Questions