如何在Python中运行复杂的shell命令?



如何在Python 3.8中运行以下命令并获得输出?

命令:

git remote show [URL for remote Github repo] | sed -n '/HEAD branch/s/.*: //p'

命令的目的:

从远程Github repo获取默认分支也试着在git目录

外运行这个下面是我尝试过的命令,但是它给出了错误:">fatal: not a git repository(或任何父目录):.git":

import os
p = os.system("git remote show https://github.com/docker/compose | grep 'HEAD branch' | cut -d' ' -f5")
import subprocess
subprocess.run(["git", "remote", "show", "https://github.com/docker/compose", "|", "grep", "'HEAD branch'", "|", "cut", "-d' '", "-f5" ])

我需要命令在Git目录外执行,请帮助我

要运行命令,您可以使用subprocess.run,它可以返回您想要的所有信息。

但是如果你只想从git中获取信息,可以使用现成的模块,比如GitPython来代替

最新更新