简体   繁体   中英

How do I run a bash script inside Python, but act as if it's running from another directory?

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

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

Use the cwd argument to subprocess.call()

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

If cwd is not None, the child's current directory will be changed to cwd before it is executed. Note that this directory is not considered when searching the executable, so you can't specify the program's path relative to cwd .

Example:

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

Well, you could use subprocess.Popen with Shell = True and cwd = "Your desired working directory"

EDIT: It appears that call has the same arguments so just setting a cwd argument would work:

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

You can supply your working directory like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM