0

我有一个 bash 脚本,它有助于建立一个本地SimpleHTTPServer.

python -m SimpleHTTPServer 8080

我把它放在我的项目文件夹中。当我使用以下方法运行程序时:

subprocess.call('./setup.sh')

出现一条错误消息:

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    subprocess.call('./setup.sh')
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 709, in __init__
    errread, errwrite)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1326, in _execute_child
    raise child_exception
OSError: [Errno 13] Permission denied

我在终端重试了这个

localhost:Desktop XXXX$ sh setup.sh
Serving HTTP on 0.0.0.0 port 8080 ...

它工作正常。我记得有几次终端弹出一个窗口询问我关于python的权限关于防火墙相关的事情,我允许了。你能帮助我吗?

4

2 回答 2

2

完全像在 shell 上那样运行它,即sh ./setup.sh

subprocess.call('sh ./setup.sh', shell=True)

这应该够了吧。最有可能的是,您setup.sh的未设置为可执行或缺少#!标记其解释器的第一行。

编辑:

shell=True如果您将其作为单个字符串传递,或者将参数分隔到一个列表中,请确保设置为通过 shell 执行它,就像您可能使用的那样execve

subprocess.call(['sh', './setup.sh'])
于 2013-11-28T07:01:54.177 回答
1

试一试subprocess.Popen(),使用 cwd 参数:

subprocess.Popen(['sh', './setup.sh'], cwd='/dir/contains/setup.sh/')
于 2013-11-28T07:19:01.487 回答