【发布时间】:2011-04-29 00:00:23
【问题描述】:
我有一个由第 3 方提供的 bash 脚本,它定义了一组函数。这是一个看起来像的模板
$ cat test.sh
#!/bin/bash
define go() {
echo "hello"
}
我可以从 bash shell 执行以下操作来调用 go():
$ source test.sh
$ go
hello
有没有办法从 python 脚本访问相同的函数?我尝试了以下方法,但没有成功:
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call("source test.sh")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/subprocess.py", line 470, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.6/subprocess.py", line 623, in __init__
errread, errwrite)
File "/usr/lib/python2.6/subprocess.py", line 1141, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
>>>
【问题讨论】:
-
bash中有什么功能不能在Python中复制?