Passing a list to a subprocess call

2019-09-17 20:02发布

问题:

I have a problem with trying to pass a list into a subprocess call command. I am trying to call the windows robocopy function, passing a list of file types that it should filter by.

filter_list = ['*.txt', '*.dat']
call(["robocopy", src, dst, filter_list, "/e"])

So passing the list itself does not work the output from robocopy showed that it was trying to find the file type ".txt.dat" as if the whole list were a single file type.

I then tried the following

call(["robocopy", src, dst, ','.join(filter_list), "/e"])

However that gave the same output as my first attempt. Does anyone know how to pass in a list and have it properly divided? Any help is much appriciated!

回答1:

You should actually pass the arguments in the list:

args = ["robocopy", src, dst]
args.extend(filter_list)
args.append("/e")
call(args)