How to run a command with %?


I am trying to run command git log origin/master..HEAD --format=format:"%H" in python as below but running into below error,I tried to escape % but that doesn’t fix the error, any idea how to fix it?

def runCmd2(cmd):"Running command %s"%cmd)
    proc = Popen(cmd ,universal_newlines = True, shell=True, stdout=PIPE, stderr=PIPE)
    (output, error) = proc.communicate()
    return output.strip(),error.strip()

def get_local_commits():
    """Get local commits """
    branch = "master"
    cmd = "git log origin/%s..HEAD  --format=format:\"%H\" "%(branch)
    output,error = runCmd2(cmd)
    return output,error


  File "/Users/gnakkala/jitsuin/wifi-ci/", line 45, in get_local_commits
    cmd = "git log origin/%s..HEAD  --format=format:\"%H\" "%(branch)
TypeError: not enough arguments for format string

>Solution :

To escape % you double it, using %%

branch = "master"
cmd = 'git log origin/%s..HEAD  --format=format:"%%H"' % branch

Leave a Reply Cancel reply