将命令提示数据分配到Python变量中



我正在尝试检索subversion存储库中文件的SVN日志信息,后来需要将其倒入CSV。

显然,我使用Python OS软件包运行以下命令

代码:

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

我在goody.txt中获取了SVN日志数据,但是为多个文件执行此类操作(写给TXT和从TXT上阅读(非常耗时。

是否有一种方法可以将svn log -v获得的数据自动分配到Python变量?

您可以使用subprocess.popen。

import subprocess
filePath = r"C:Project_FilesMy_Projectfile1.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()

一个工作示例将为

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)

这里的输出将为

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

最新更新