Home > front end >  How to run a command with %?
How to run a command with %?

Time:01-25

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):
    logger.info("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

ERROR:-

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

CodePudding user response:

To escape % you double it, using %%

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

CodePudding user response:

% interpolation is outdated

use f-strings instead!

def runCmd2(cmd):
    logger.info(f'Running command {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 = f'git log origin/{branch}..HEAD  --format=format:\"{'%H'}\"'
    output,error = runCmd2(cmd)
    return output,error
  • Related