The error message does not correspond to your code: the code in question would produce SyntaxError, not TypeError.
You don't need shell=True. To get git's output, you could use subprocess.check_output() function:
from subprocess import check_output
date_string = check_output('git log -1 --pretty=format:"%ci"'.split()).decode()
Answer from jfs on Stack OverflowThe error message does not correspond to your code: the code in question would produce SyntaxError, not TypeError.
You don't need shell=True. To get git's output, you could use subprocess.check_output() function:
from subprocess import check_output
date_string = check_output('git log -1 --pretty=format:"%ci"'.split()).decode()
The code is missing ,, and there is an extra ):
proc = subprocess.Popen(
'git log -1 --pretty=format:"%cd"',
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = proc.stdout.read()
proc.wait()
After getting the output of the command, you can use datetime.datetime.strptime to convert the string to datetime object, and convert it to the format you like using datetime.datetime.strftime:
import datetime
dt = datetime.datetime.strptime(output.rsplit(None, 1)[0], '%a %b %d %H:%M:%S %Y')
print(output)
print(dt.strftime('%Y-%m-%d'))
git - Python subprocess yielding a different result than shell? - Stack Overflow
Why are Git log command outputs different when launched from Windows shell and from Python subprocess? - Stack Overflow
Python subprocess - git log wrong JSON Format - Stack Overflow
How to use git command 'git log -p' with GitPython
When you want to parse git output, you should use the form of its commands intended for scripting. In this case it means the --pretty=format:1 option. The %D specifier provides the ref names pointing to the commit, and of course you need to explicitly specify all the fields you want.
You might also want to use the lower level git rev-list command instead of log; it has similar options, but is promised to have stable interface for script use.
Add option --decorate:
subprocess.check_output(['git', 'log', '--decorate', '--no-walk', '--tags', '--reverse'], stderr=subprocess.PIPE).decode(sys.stdout.encoding)
Seems like the new line is not right, try:
sb.Popen(['git' , 'diff' ,'--name-status' ,test.strip(), 'HEAD'])
Your test variable has a trailing newline, strip it and it will work fine
import subprocess as sb
commit_id = sb.Popen(['git', 'merge-base' ,'FETCH_HEAD', 'HEAD'], stdout=sb.PIPE)
test=commit_id.communicate()[0]
print(test)
sb.Popen(['git' , 'diff' ,'--name-status' ,test[:-1], 'HEAD'])
Have you considered using GitPython? It's designed to handle all this nonsense for you.
import git # pip install gitpython
g = git.cmd.Git(git_dir)
g.pull()
To install the module use pip install GitPython
Project can be found here, GitPython
subprocess.Popen expects a list of the program name and arguments. You're passing it a single string, which is (with the default shell=False) equivalent to:
['git pull']
That means that subprocess tries to find a program named literally git pull, and fails to do so: In Python 3.3, your code raises the exception FileNotFoundError: [Errno 2] No such file or directory: 'git pull'. Instead, pass in a list, like this:
import subprocess
process = subprocess.Popen(["git", "pull"], stdout=subprocess.PIPE)
output = process.communicate()[0]
By the way, in Python 2.7+, you can simplify this code with the check_output convenience function:
import subprocess
output = subprocess.check_output(["git", "pull"])
Also, to use git functionality, it's by no way necessary (albeit simple and portable) to call the git binary. Consider using git-python or Dulwich.
You need to specify the working directory.
Functions Popen, call, check_call, and check_output have a cwd keyword argument to do so, e.g.:
subprocess.call([gitPath] + dirList + ['add','.'], cwd='/home/me/workdir')
See also Specify working directory for popen
Other than using cwd Popen's argument, you could also use git's flag -C:
usage: git [--version] [--help] [-C <path>] [-c name=value]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
So that it should be something like
subprocess.Popen('git -C <path>'...)