Passing a list to a subprocess call

扶醉桌前 提交于 2019-12-13 07:43:50

问题


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)


来源:https://stackoverflow.com/questions/30071237/passing-a-list-to-a-subprocess-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!