How to run a command with %?
Question:
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
Answers:
To escape % you double it, using %%
branch = "master"
cmd = 'git log origin/%s..HEAD --format=format:"%%H"' % branch
% 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
You shouldn’t be using the shell at all here, which makes the problem go away.
def runCmd2(cmd: list[str):
logger.info("Running command %s"%(' '.join(cmd))
proc = Popen(cmd, universal_newlines=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/{}..HEAD".format(branch), '--format=format:%H']
output,error = runCmd2(cmd)
return output,error
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
To escape % you double it, using %%
branch = "master"
cmd = 'git log origin/%s..HEAD --format=format:"%%H"' % branch
% 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
You shouldn’t be using the shell at all here, which makes the problem go away.
def runCmd2(cmd: list[str):
logger.info("Running command %s"%(' '.join(cmd))
proc = Popen(cmd, universal_newlines=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/{}..HEAD".format(branch), '--format=format:%H']
output,error = runCmd2(cmd)
return output,error