python - 如何检查命令是否完成?

标签 python linux python-3.x

我正在尝试用 git 做一些事情。我想知道如何制作 if 语句 参数来检查该命令是否正确完成:

git checkout master && git reset --hard && git fetch && git pull

我正在尝试检查输出是否等于 if 语句,但我想这是个坏主意。这是一个代码:

#!/usr/bin/python3
import os
import subprocess
import time

while True:

x = subprocess.getoutput("git checkout master && git reset --hard && git fetch && git pull")

   if x is "Some text":
    print('Some actions there')

time.sleep(3600)

所以我想知道有什么方法可以检查命令是否完成。

最佳答案

使用subprocess模块,但使用 Popen 执行每个命令或 run .我会推荐后者,因为它的返回类型很有用 check_returncode()方法。使用 shlex.split()将命令转换为其部分列表。

基本上你想做的是这样的:

import subprocess, shlex
for (cmd in ("git checkout master", "git reset --hard", "git fetch", "git pull")):
    subprocess.run(shlex.split(cmd)).check_returncode()

免责声明:这是临时编写的,未经测试。它只是应该告诉你如何到达那里,而不是一路为你做那件事。

关于python - 如何检查命令是否完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40006653/

相关文章:

python - Numpy,数组没有自己的数据?

linux - 库大小如何影响应用程序的加载时间和内存占用量?

Linux 中的 Java 命令类路径和 PHP

python - Pandas 过滤 : Returning True/False vs the Actual Value

python - 在 Python >= 3.2 中将缓存存储到文件 functools.lru_cache

python - 在 django 中按 ManyToMany 关系列出标签

python - 如何将查询中设置的page_size设置为分页元数据djangorest框架

python - 从 python 列表中选择子列表,在同一元素上开始和结束

linux - PureBasic - 我如何模拟按下 F1 或 F2 键盘按钮?

python - 将请求response.content放入队列后multiprocessing.Process不会终止