开发者

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

开发者 https://www.devze.com 2023-02-07 03:39 出处:网络
subprocess.call([\"/home/blah/trunk/blah/run.sh\",开发者_Go百科 \"/tmp/ad_xml\", \"/tmp/video_xml\"])
subprocess.call(["/home/blah/trunk/blah/run.sh",开发者_Go百科 "/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")


os.chdir(path)

http://docs.python.org/library/os.html#os.chdir

0

精彩评论

暂无评论...
验证码 换一张
取 消