git show不使用python check_outpu

2024-04-27 00:31:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个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
>>>

直接运行:

^{pr2}$

有一点需要指出的是git showvi样式输出,即不是直接打印完整的文件,而是打印文件的一部分,以覆盖完整的屏幕,在最后用:来打印下一行或退出。在

为什么我在使用check_output时出错?在


Tags: inpygitoutputlibcheckshowline
1条回答
网友
1楼 · 发布于 2024-04-27 00:31:39

试试这个:

subprocess.check_output(['git', 'show', 'c9a89aa:supertext/src/com/stxt/supercenter/rest/api/bootstrap/BootstrapDTO.java'])

否则,代码将尝试执行一个字面上包含空格(“git show”)的命令,而不是以show作为第一个参数的git命令。在

相关问题 更多 >