2014-06-15 73 views
0

我试图获取标签的日期。该cmd命令,我知道的是:使用python和p4v获取标签的日期

p4 labels -e the_label_name 

的确实给了我下面的:

Label the_label_name 2014/06/05 00:05:13 'Created by mebel. ' 

使用Python,我写道:

os.system("sc labels -t -e the_label_name") 

什么,我得到的是:

Label the_label_name 2014/06/05 00:05:13 'Created by mebel. ' 

0 

但是,如果我写

label = os.system("sc labels -t -e the_label_name") 

我得到

label = 0 

你知道我缺少什么?

+0

正如你的第一个例子清楚显示的那样,'os.system'调用的返回值是'0'。这被分配给'label'。根据[文档](https://docs.python.org/2/library/os.html#os.system),如果您想要检索进程的结果,请考虑使用'subprocess'。 – jonrsharpe

+1

考虑使用P4Python库;它使脚本Perforce调用更容易。 –

+0

您也可以使用-G全局选项,该选项可以将所有输出(以及带有-i的表单命令的批量输入)格式化为封送Python字典对象,这在脚本编写时最常使用。请参阅http://www.perforce.com/perforce/doc.current/manuals/cmdref/global.options.html。 –

回答

0

我发现这一点:

label = os.popen("sc labels -e the_label_name") 
label = label.read() 

修复了一切......

0

根据os.system的文档,返回值是程序的退出状态。

如果你要检索的程序的输出,你可以使用check_output功能从subprocess

import subprocess 
label = subprocess.check_output("sc labels -t -e the_label_name", shell=True) 

例子:

>>> import subprocess 
>>> subprocess.check_output("shuf -n 1 /usr/share/dict/words", shell=True) 
>>> 'monkey-pot\n' 
+0

我试过这个,得到:'Traceback(最近调用最后一个): 文件“”,第1行,在 文件“/org/seg/tools/freeware/python/2.7.1/1/el-5 -x86_64/lib/python2.7/subprocess.py“,第530行,check_output process = Popen(stdout = PIPE,* popenargs,** kwargs) 文件”/org/seg/tools/freeware/python/2.7 .1/1/el-5-x86_64/lib/python2.7/subprocess.py“,第672行,在__init__中 errread,errwrite) 文件”/org/seg/tools/freeware/python/2.7.1/ 1/el-5-x86_64/lib/python2.7/subprocess.py“,行1202,在_execute_child raise child_exception OSError:[Errno 2]没有这样的文件或目录 ' – NimrodB

+0

对,你应该使用shell = TRUE',或者将命令作为字符串列表传递:'['sc','labels','-t','-e','the_label_name']'。 – eskaev