0

I am trying to retrieve svn log information of a file in a subversion repository, which later needs to be dumped into a CSV.

Apparently, I run the following command using python os package

Code:

filePath = r"C:\Project_Files\My_Project\file1.c" # My_Project is in Subversion
svn_command = "svn log " + filePath + "-v > C:\\information.txt"
os.system(svn_command)

I get the svn log data in the information.txt but performing this kind of operation for several files (writing to txt and reading from txt) is very time consuming.

Is there a way to automatically assign the data obtained from svn log -v into a python variable?

3
  • Hi @SuryaTej, you can use subprocess.Popen for this, check my answer below :) Commented May 14, 2019 at 9:53
  • Thank you @DeveshKumarSingh your subprocess.Popen approach worked for me Commented May 14, 2019 at 14:59
  • Glad to help @SuryaTej and thanks for the accept :) Commented May 14, 2019 at 15:07

1 Answer 1

1

You can use subprocess.Popen for this, note that I am splitting up the command into a list, you can create that list beforehand

import subprocess

filePath = r"C:\Project_Files\My_Project\file1.c" # My_Project is in Subversion
svn_command = "svn log " + filePath + "-v > C:\\information.txt"

#Split command into individual words
cmd_list = svn_command.split()

#Run command via subprocess Popen
cmd_output = subprocess.Popen(cmd_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

#Get output and error 
cmd_output, cmd_err = cmd_output.communicate()

#Get output and error string
cmd_output_str = cmd_output.decode('utf-8', errors='ignore').strip()
cmd_err_str = cmd_err.decode('utf-8', errors='ignore').strip()

A working example will be

import subprocess

cmd = 'uname -a'

#Split command into individual words
cmd_list = cmd.split()

#Run command via subprocess Popen
cmd_output = subprocess.Popen(cmd_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

#Get output and error
cmd_output, cmd_err = cmd_output.communicate()

#Get output and error string
cmd_output_str = cmd_output.decode('utf-8', errors='ignore').strip()
cmd_err_str = cmd_err.decode('utf-8', errors='ignore').strip()

print(cmd_output_str)
print(cmd_err_str)

The output here will be

Darwin LUSC02WK0GKHTDH 18.5.0 Darwin Kernel Version 18.5.0: Mon Mar 11 20:40:32 PDT 2019; root:xnu-4903.251.3~3/RELEASE_X86_64 x86_64

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.