Python子进程check_ouput在第356行和第438行吐出错误

问题描述 投票:0回答:1

我正在尝试使用python子进程check_output命令编写git hub pull,push和status命令的脚本,如下所示。有人可以让我知道这些错误是什么以及如何平稳执行该脚本。仅当没有更多文件可提交时才会发生这种情况。

导入操作系统从子流程导入check_output
gitzap =['git pull','git add *','git commit -m "all"','git push','git status']

for command in gitzap:
    #print (command)
    # executioner = os.system(command).pop()
    executioner = check_output(command, stdin=None, stderr=None, shell=True, universal_newlines=True, timeout=None, encoding='utf-8')
    print ('\n' + str(command) + '\n***************success***************\n')
    print (str(executioner))

输出如下:

〜/ documents / github / goodcode / githelpcommands $ gitzapgit pull***************成功***************已经是最新的。git添加****************成功***************追溯(最近一次通话):中的文件“ /home/xxxxxxxxxx/documents/github/goodcode/githelpcommands/gitzap.py”,第10行executeer = check_output(命令,stdin =无,stderr =无,shell = True,Universal_newlines = True,超时=无,编码='utf-8')在check_output中的文件“ /usr/lib/python3.6/subprocess.py”,行356** kwargs).stdout运行中的文件“ /usr/lib/python3.6/subprocess.py”,第438行输出=标准输出,标准错误=标准错误)subprocess.CalledProcessError:命令'git commit -m“ all”返回非零退出状态1。
python python-3.x subprocess
1个回答
0
投票

您的输出和发生时间的描述会告诉您确切的错误是什么。

git commit将在没有要提交的文件时返回非零退出状态代码,因此您得到:

subprocess.CalledProcessError: Command 'git commit -m "all"' returned non-zero exit status 1.

您必须先检查是否有文件要提交。例如,通过快速搜索-您可以使用this线程中的解决方案。

虽然我会去处理错误。也许创建一个处理程序,为不同的命令打印不同的错误消息?

© www.soinside.com 2019 - 2024. All rights reserved.