When converting from bash script to python you can go two ways:
you take over the program calls and just replace the looping / string processing, e.g. like this:
from subprocess import check_output
for line in check_output('git branch -r', shell=True).split("\n"):
line = line.strip()
# do your replaces here, etc. leaving that for an exercise for you
# run more `check_call` or `check_output` here
you do it "the right way" and use a e.g. a python git module. That's initially more work, but long term beneficial as you get more out of the box features, and have less bugs as in your "quickly hacked together shell calls"
Edit: As a commenter rightly suggests, you should try to avoid shell=True
. In this case, check_output(['git', 'branch', '-r'])
is certainly better, in other cases, when you know that the python script will be run on a Linux systems and under a specific shell (e.g. bash), then having shell=True
allows you to access environment variables specified under .bashrc
, do globbing, etc. For a detailed discussion see here.