【发布时间】:2021-02-18 04:19:54
【问题描述】:
我通过 subprocess.check_output 使用 bash 命令从 git 获取一些分支,对其进行解码、剥离,然后将其拆分为一个列表。
然后我将遍历列表以检查哪些分支不符合要求,如果是这种情况,则将它们从列表中删除。但由于某种原因,它没有按预期工作。
#Comes back as a bytes object with newlines after each branch name
git = subprocess.check_output(["git", "branch", "-r"])
branches = git.decode("utf-8").replace("origin/", "").strip("\n").split()
for branch in branches:
if "LIVE" not in branch:
branches.remove(branch)
print(branches)
最后,我希望分支会以空列表的形式返回,因为我知道没有一个分支包含“LIVE”,因此应该从列表中删除。但是,列表仍然每次返回 1/2 个元素,我不知道为什么。
【问题讨论】:
标签: python python-3.x git