2

The paths of the files I want to process contain parentheses in them.

path = "/dir/file (with parentheses).txt"

I'm trying to process them in Python as follows:

subprocess.call("./process %s" % path, shell=True)

However, I get the following error

/bin/sh: 1: Syntax error: "(" unexpected

How can I pass the correct string to process the proper path?

Olivier
  • 1,821
  • 5
  • 23
  • 29
  • 2
    Do you need shell=True? If not it will likely work fine with making the args a list and not letting the shell trip on special characters – Eric Renouf Jun 01 '15 at 03:00

2 Answers2

5

Don't use shell=True. It's trouble-prone (as in the OP) and enables shell injection attacks.

Do it like this:

subprocess.call(["./process", path])

If you insist on using shell=True, read the Security Considerations in the python documentation, and make sure you use shlex.quote to correctly escape all metacharacters.

rici
  • 219,119
  • 25
  • 219
  • 314
-2

Try this

subprocess.call('./process "%s"' % path, shell=True)

I guess problem is more with space in file name. File names with spaces in them should be enclosed in quotes like this ./process "foo bar.txt" or escaped like this ./process foo\ bar.txt.

f43d65
  • 2,150
  • 9
  • 14