擅长:python、mysql、java
<p>Popen中的相对路径相对于当前工作目录,而不是系统路径的元素。如果从<code>/dir</code>运行<code>python subdir2/some_script.py</code>,则预期的可执行位置将是<code>/dir/../subdir2/some_executable</code>,也就是<code>/subdir2/some_executable</code>。</p>
<p>如果您确实希望使用从脚本自己的目录到特定可执行文件的相对路径,最好的选择是首先从<code>__file__</code>全局变量的目录部分构造绝对路径。</p>
<pre><code>#/usr/bin/env python
from subprocess import Popen, PIPE
from os.path import abspath, dirname, join
path = abspath(join(dirname(__file__), '../subdir1/some_executable'))
spam, eggs = Popen(path, stdout=PIPE, stderr=PIPE).communicate()
</code></pre>