0
votes

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?

2

2 Answers

1
votes

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.

1
votes

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.