如何使用python获取Beyond Compare exe完整路径?

How to get Beyond Compare exe full path using python?

我正在尝试使用以下代码查找 Beyond Compare exe 的完整路径。但是两者都将输出作为 None.

from shutil import which
print(which('BCompare'))

from distutils import spawn
print(spawn.find_executable('BCompare'))

预期输出:

C:\Program Files\Beyond Compare 4\BCompare.exe

提前致谢

因为你的 Beyond Compare 安装在 C:\Program Files\Beyond Compare 4,试试这个:

from os import environ
from pathlib import Path

location = r'C:\Program Files\Beyond Compare 4'
dirs_on_path = [dir.strip('\') for dir in environ['path'].split(';')]

if location in dirs_on_path:
    print('directory is on the path')
    if (Path(location) / 'BCompare.exe').is_file():
        print('and executable could be found, which("BCompare") should work')
    else:
        print('but executable could not be found')
else:
   print('directory is not on the path')

要么你的程序位置不在路径上(一个系统环境变量,更多关于如何在此处更改它的信息 - 只是我从 Google https://www.architectryan.com/2018/03/17/add-to-the-path-on-windows-10/ 中选择的最高结果)

或者位置在路径上,但可执行文件不在。该脚本会测试两者并让您知道 - 这应该可以让您找出问题所在。