16

I'm trying to create a program that scans a text file and passes arguments to subprocess. Everything works fine until I get directories with spaces in the path.

My split method, which breaks down the arguments trips up over the spaces:

s = "svn move folder/hello\ world anotherfolder/hello\ world"

task = s.split(" ")
process = subprocess.check_call(task, shell = False)

Do, either I need function to parse the correct arguments, or I pass the whole string to the subprocess without breaking it down first.

I'm a little lost though.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
james_dean
  • 1,477
  • 6
  • 26
  • 37

1 Answers1

23

Use a list instead:

task = ["svn",  "move",  "folder/hello world", "anotherfolder/hello world"]
subprocess.check_call(task)

If your file contains whole commands, not just paths then you could try shlex.split():

task = shlex.split(s)
subprocess.check_call(task)
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • 1
    thank you thank you! using a list solves all sorts of weird quoting/escaping issues +1 – Brian Feb 11 '17 at 20:17