0

i am using this:

output2 = subprocess.check_output("find /users/experiment_in14 -empty", shell=True)

which perfectly working for me but I would like to replace the full path in order to change the path only once at the beginning of the code and not everywhere. I am thinking something like this:

original = /users/experiment_in14 
output2 = subprocess.check_output("find ,original, -empty", shell=True)
print output2

but it doesn't work. which is the right way to replace the path?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
kostas
  • 5
  • 5

1 Answers1

0

You can use string formatting:

original = "/users/experiment_in14"
output2 = subprocess.check_output("find {} -empty".format(original), shell=True)
print output2

but you need to be careful with paths that contain shell metacharacters. You'd have to make sure the path doesn't use metacharacters or explicitly quote the value:

import pipes

original = "/users/experiment_in14"
output2 = subprocess.check_output("find {} -empty".format(pipes.quote(original)), shell=True)
print output2

You don't actually need to use shell=True here; you can head off any problems by passing in the arguments separately and not use the shell:

original = "/users/experiment_in14"
output2 = subprocess.check_output(['find', original, '-empty'])
print output2

Here original is just one more separate argument and won't need quoting.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343