【问题标题】:running a simple ls command with subprocess and git bash使用 subprocess 和 git bash 运行一个简单的 ls 命令
【发布时间】:2019-12-29 14:04:20
【问题描述】:

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

    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窗口,窗口中的标题为-/usr/bin/bash --login -i ls,在git bash窗口中错误为bash:/usr/bin/ls: cannot execute binary file。

我也试过了——

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

但错误是一样的。

【问题讨论】:

    标签: python-3.x subprocess git-bash


    【解决方案1】:

    应该使用-c参数:

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

    Git for Windows 带有bash.exe。是is in the %PATH%,应该够了。

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 2016-06-18
      • 2016-12-30
      • 2016-05-13
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-11
      相关资源
      最近更新 更多