sh命令和fi读取的变量不一致

2024-04-25 22:55:08 发布

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

我有一个问题,使用sh模块获得的字符串与git和写入文件的字符串不匹配

我得到如下提交字符串

from sh import stat, git
repo = git.bake(_cwd=git_dir)
current_commit=git.log("-n1", "--format='%T'", git_file)

当前提交如下所示

print current_commit
'89a848eb9ea98bfd6770301dce848052ec8ef63f'

类型如下

type(current_commit)
<class 'sh.RunningCommand'>

我可以把它改成一根弦,它的长度是66

type(str(current_commit))
<type 'str'>
len(str(current_commit))
66

与实际字符串的长度不同

len('89a848eb9ea98bfd6770301dce848052ec8ef63f')
40

当前的提交被写入一个文件,如下所示

with open(commit_store_file, 'w+') as f:
        f.write(str(current_commit))

当我用vi打开这个文件时,我看到了以下内容

^[[?1h^[=^M'89a848eb9ea98bfd6770301dce848052ec8ef63f'^[[m
^M^[[K^[[?1l^[>

我把它和下面一起读了回来

with open(commit_store_file, 'r+') as f:
    stored_commit = f.readline()

这里的类型是string

type(stored_commit)
<type 'str'>

长度是55

print len(stored_commit)
55

如何使用sh检索实际字符串并将其正确存储以便它们匹配?你知道吗

谢谢


Tags: 文件字符串git类型lentypeshwith
1条回答
网友
1楼 · 发布于 2024-04-25 22:55:08

那些角色在我看来像寻呼机。你知道吗

尝试将 no-pager添加到命令的前面:

    git  no-pager log -n 1  format='%T'  color=never

该选项需要放在log命令之前,而不是后面,因此您必须bake将其放入。获得相同效果的另一种方法是使用GIT_PAGER环境变量或配置设置。你知道吗

获取机器可读树散列的另一种方法是git cat-file -p HEAD。树将出现在输出的第一行,前缀为“tree”。你知道吗

相关问题 更多 >