简体   繁体   中英

How to run complex shell command in Python?

How can I run the below command in Python 3.8 and get the output?

Command:

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

Command purpose:

To get the default branch from remote Github repo also trying to run this outside git directory

Below are the commands I have tried till now, but it gives error: " fatal: not a git repository (or any of the parent directories): .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" ])

I need the command to execute even outside the Git directory, please help me

To run a command you can use subprocess.run which can return all the information you want.

But if you just want information from git use a ready-made module like GitPython instead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM