Python:将egrep输出附加到fi中

2024-05-29 06:49:43 发布

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

在Python脚本中,我试图计算{H9,H10…C21,H11}在一组文件中出现的次数。我不需要为集合中的每个短语计数,只需要一个总和。文件名包含在名为“legend”的数组中,并按名称进行数字排序。 我的最佳尝试如下:

for nitem in sorted(legend, key=numericalSort):
    with open(nitem, 'a+') as f:
        with open('block.txt', 'a+') as myfile:
            var5 = 'egrep -c "H9|H10|N71|N61|H32|C81|N91|C51|C61|C41|N11|N31|C21|H11" <%s> %s' %(nitem,myfile)
            os.system(var5)

运行此操作时,对于图例中包含的每个文件,我都会收到以下错误(下面只包含一条错误消息):

sh: -c: line 0: syntax error near unexpected token `<'
sh: -c: line 0: `egrep -c "H9|H10|N71|N61|H32|C81|N91|C51|C61|C41|N11|N31|C21|H11" <file00.dat> <open file 'block.txt', mode 'a+' at 0x7f71196ec660>'

我只想在python中附加egrep,但不能这样做。你知道吗


Tags: 文件txtaswithopenblockmyfilelegend
1条回答
网友
1楼 · 发布于 2024-05-29 06:49:43

我找到了一个方法:

for nitem in sorted(legend, key=numericalSort):
    with open(nitem, 'a+') as f:
        var5 = '''egrep -c "H9|H10|N71|N61|H32|C81|N91|C51|C61|C41|N11|N31|C21|H11" %s >> filename.txt''' %(nitem)
        os.system(var5)

使用三重引号和不混淆python append for UNIX append似乎可以做到这一点。你知道吗

相关问题 更多 >

    热门问题