我有一个bash片段,我想移植到Python。它发现SVN的位置以及它是否可执行。如何在Python中使用这个bash测试构造?
SVN=`which svn 2>&1`
if [[ ! -x $SVN ]]; then
echo "A subversion binary could not be found ($SVN)"
fi
下面是一个使用子模块在Python我当前的尝试:
SVN = Popen('which svn 2>&1', shell=True, stdout=PIPE).communicate()[0]
Popen("if [[ ! -x SVN ]]; then echo 'svn could not be found or executed'; fi", shell=True)
这不,因为虽然我有SVN的位置保存在Python的本地命名空间的工作,我可以”从Popen访问它。
我也试图组合成一个POPEN对象:
Popen("if [[ ! -x 'which svn 2>&1']]; then echo 'svn could not be found'; fi", shell=True)
,但我得到这个错误(不用说,看起来很笨重)
/bin/sh: -c: line 0: syntax error near `;'
/bin/sh: -c: line 0: `if [[ ! -x 'which svn 2>&1']]; then echo 'svn could not be found'; fi'
有一个测试的Python版本构造“-x”?我认为这将是理想的。其他解决方法也将受到赞赏。
由于
[此网站](http://ubuntuforums.org/showthread.php?t=1457094)提供了一个代码段,看起来像'commands.getoutput(“如果[-x MYFILE] \ n然后回声真\ NFI“)'。然而,由于您仍在调用Bash,因此这很难“移植到Python”。 – Kos 2013-03-11 07:14:04
'os.stat'可以给你关于给定文件的一些信息,比如它的权限,但是我认为你仍然需要为它建立一个“可执行的当前用户”测试。 – Kos 2013-03-11 07:16:17
您可以先将bash命令存储为字符串,以便您可以将它与变量SVN连接起来?然后将其传递给Popen()... – Jeff 2013-03-11 07:16:58