gpt4 book ai didi

python - 通过将 git 命令传递给 python 中的子进程来获取最后一次 git 提交日期

转载 作者:太空宇宙 更新时间:2023-11-03 17:31:36 24 4
gpt4 key购买 nike

我有一个脚本,我只需要检索最后一次 git 提交的 2015-07-28 格式的日期。

但是如果我得到 Tue Jul 28 16:23:24 2015 +0530 然后在终端中使用 git log -1 --pretty=format:"%ci"如果我想尝试

将其作为字符串传递给 subprocess.Popen 就像

subprocess.Popen('git log -1 --pretty=format:"%cd"' shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE))

但这会引发错误TypeError:%c需要int或char,我猜想Python的东西我们将一个char传递给%c,而这是为了使用git命令获取日期。

我需要将此日期连接到我的 python 脚本中的字符串。

最佳答案

错误消息与您的代码不对应:相关代码将产生 SyntaxError,而不是 TypeError

您不需要shell=True。要获取 git 的输出,您可以使用 subprocess.check_output() function :

from subprocess import check_output

date_string = check_output('git log -1 --pretty=format:"%ci"'.split()).decode()

关于python - 通过将 git 命令传递给 python 中的子进程来获取最后一次 git 提交日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31761788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com