运行 带有子进程和 git bash 的简单 ls 命令

running a simple ls command with subprocess and git bash

我正在尝试从 Windows 操作系统内部调用 git bash shell 命令。以下是我的代码片段。谁能告诉我,我做错了什么?

    git_bash_path = WindowsPath(r"c:\Users\abcd\AppData\Local\Programs\Git\git-bash.exe")
    command = "ls"
    subprocess.call([str(git_bash_path), command])

在 运行、python 脚本之后,它打开一个 bash window,标题在 window - /usr/bin/bash --login -i ls 和在 git bash window 中,错误是 bash:/usr/bin/ls: 无法执行二进制文件。

我也试过了 -

    subprocess.check_output(command, shell=True, executable=str(git_bash_path)) 

但错误是一样的

应该使用-c参数:

bashCommand = "ls"
output = subprocess.check_output(['bash','-c', bashCommand])

Git for Windows comes with a bash.exe. It is is in the %PATH%,应该够了。