How to run a bash script inside Python, but act as if it were being run from another directory?

subprocess.call(["/home/blah/trunk/blah/run.sh", "/tmp/ad_xml", "/tmp/video_xml"])

I'm doing it. However, inside my run.sh, I have "relative" paths. So, I have to "cd" into this directory, and then run the shell script. How to do it?

+5
source share
4 answers

Use argument cwdtosubprocess.call()

From the docs here: http://docs.python.org/library/subprocess.html

cwd None, childs cwd . , , , cwd.

:

subprocess.call(["/home/blah/trunk/blah/run.sh", "/tmp/ad_xml", "/tmp/video_xml"], cwd='/tmp')
+12

, subprocess.Popen Shell = True cwd = " "

EDIT: , , cwd:

subprocess.call(["/home/blah/trunk/blah/run.sh", "/tmp/ad_xml", "/tmp/video_xml"], cwd="PATH")
+1

:

subprocess.call([ "/home/blah/trunk/blah/run.sh", "/tmp/ad_xml", "/tmp/video_xml" ], cwd = "/home/blah/trunk/blah" )

+1

All Articles