我正在编写一个python脚本,其中我必须从脚本中读取git show命令的输出。我决定使用python的subprocess.check_output函数。
但这给了我No such file or directory错误。
从python跑
>>> import subprocess
>>> subprocess.check_output(['pwd'])
'/Users/aapa/Projects/supertext\n'
>>> subprocess.check_output(['git show', 'c9a89aa:supertext/src/com/stxt/supercenter/rest/api/bootstrap/BootstrapDTO.java'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 566, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 709, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1326, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
>>>直接运行
$ pwd
/Users/aapa/Projects/supertext
$ git show c9a89aa:supertext/src/com/stxt/supercenter/rest/api/bootstrap/BootstrapDTO.java
package com.stxt.supercenter.rest.api.bootstrap;
import com.google.common.collect.Maps;
import com.stxt.base.rolepermission.enums.Role;
import com.stxt.superbase.profile.agent.bean.Agent;
import com.stxt.supercenter.rest.api.profile.agnet.AgentDTO;
import java.util.Arrays;
import java.util.Map;
.
.
.有一点要指出的是,git show输出采用vi样式,即不是直接打印完整的文件,而是打印文件的一部分以覆盖完整的屏幕,最后用:打印下一行或退出。
为什么我使用check_output时出错
发布于 2015-09-21 20:27:11
试试这个:
subprocess.check_output(['git', 'show', 'c9a89aa:supertext/src/com/stxt/supercenter/rest/api/bootstrap/BootstrapDTO.java'])否则,您的代码将尝试执行一个包含空格("git show")的命令,而不是以show作为其第一个参数的命令git。
https://stackoverflow.com/questions/32703953
复制相似问题