python - replace path in subprocess.check_output() -


i using this:

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

which working me replace full path in order change path once @ beginning of code , not everywhere. thinking this:

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

but doesn't work. right way replace path?

you can use string formatting:

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

but need careful paths contain shell metacharacters. you'd have make sure path doesn't use metacharacters or explicitly quote value:

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

you don't need use shell=true here; can head off problems passing in arguments separately , not use shell:

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

here original 1 more separate argument , won't need quoting.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -