Trying to execute git command using python script

I am trying to execute a simple git command using the following python script.

#!/usr/bin/python

import commands
import subprocess
import os
import sys

pr = subprocess.Popen( "/usr/bin/git log" , cwd = os.path.dirname( '/ext/home/rakesh.kumar/workspace/myproject' ), shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
(out, error) = pr.communicate()


print "Error : " + str(error) 
print "out : " + str(out)

but I get the following error even if I run the python script in the same directory where there is git reposetory.

Error : fatal: Not a git repository (or any of the parent directories): .git

I suspected that git might be corrected, but git files are great, and git commands work if I run on a regular command line.

I tried to search the net but could not get useful information. Please help, he will be very grateful.

+5
source share
2 answers

Try the following:

#!/usr/bin/python

import commands
import subprocess
import os
import sys

pr = subprocess.Popen( "/usr/bin/git log" , cwd = os.path.dirname( '/ext/home/rakesh.kumar/workspace/myproject/' ), shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
(out, error) = pr.communicate()


print "Error : " + str(error) 
print "out : " + str(out)

The directory path must contain a "/" at the end.

+4
source

The problem is to use os.path.dirname():

os.path.dirname( '/ext/home/rakesh.kumar/workspace/myproject' )

will provide you with:

>>> os.path.dirname( '/ext/home/rakesh.kumar/workspace/myproject' )
'/ext/home/rakesh.kumar/workspace'

which, I'm sure, is not what you want.

+5
source