0

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!

Lord_Migit
  • 51
  • 1
  • 1
  • 5

1 Answers1

4

You should actually pass the arguments in the list:

args = ["robocopy", src, dst]
args.extend(filter_list)
args.append("/e")
call(args)
Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116
  • Ahh, the append and extend were things that I did not know about. Many thanks, that seems to have worked! :) – Lord_Migit May 06 '15 at 08:49
  • `robocopy` name suggests that OP uses Windows where the list is converted to a string for `CreateProcess` i.e., it is *not* passed directly as `argv`. – jfs May 09 '15 at 17:06
  • @J.F.Sebastian — That's a good point. I removed the part about `argv`. Even on Windows, Python ensures that arguments are properly quoted, though, right? – Thomas Orozco May 10 '15 at 17:11
  • @ThomasOrozco: "properly quoted" may be defined by each individual program on Windows. Python uses quoting rules used by some C programs on Windows. [Follow links in this SO question](http://stackoverflow.com/a/27867015/4279) – jfs May 10 '15 at 17:45