Jason Donnald
Jason Donnald

Reputation: 2316

pass a list within a list as an argument to subprocess in python

I am trying to pass a list within a list as an argument to the subprocess. My code is:

def test():

    x= 'a'
    y = 'b'
    filename = "test.log"
    error_list = []
    error_list.append(x)
    error_list.append(y)
    para = ['1','2',str(error_list)]

    ret = subprocess.Popen([sys.executable,"script.py"]+para)
    ret.wait()

if __name__ == '__main__':
    test()

On the subprocess script side when I access this script like this:

def test(arg1,arg2,arg3):
    content = arg1 + arg2 + arg3
    print content
if __name__ == "__main__":

    arg1 = sys.argv[1]
    arg2 = sys.argv[2]
    arg3 = sys.argv[3]
    test(arg1,arg2,arg3)

The first two arguments come correct in content but the third argument comes as [, ', a, ', ,, , ', b, ', ] while I want it to be just a,b

Upvotes: 0

Views: 498

Answers (1)

fferri
fferri

Reputation: 18940

It prints 12['a', 'b'] because "['a', 'b']" is the string representation of the list [x,y].

If your expected output is 12a,b, replace

para = ['1','2',str(error_list)]

with

para = ['1','2',','.join(error_list)]

Upvotes: 2

Related Questions