如何使用非常量名称打开不同文件的循环

时间:2014-01-22 14:25:03

标签: python

我试图用python分析几个文件的数据并进行统计,但是文件的名称不是常数......我正在阅读一些论坛,我认为这可能是一个很好的解决方案,但是给出了我操作%操作数的错误,我想我可能写了 有些不对劲......抱歉,我不擅长编程

#!/usr/bin/python

import numpy as np
import os, os.path
import fileinput
from glob import glob

val = 0
seq = 0


out = open('../stats.txt', 'w')
while out:

    fnames = glob('../cmsearch_all/RF(%d)_*.txt') % val #here val vhanges from 00003 to infinite without being consecutive numbers, and the * was denoting to ignore tha prt because is not constant not even on numer of characters

    for line in fileinput.input(fnames):
        w = count (line,"!Myodav*") 
        wa = count (line,"!Myotri*")
        wb = count (line,"!Tricot*") #here I was hoping to be counting the number of times !Tricot appears, but the line in the file actually looks like !Tricot_123_fromdatabase812hg243538dth but I care just when this !Tricot appears 

    fnames = glob('../cmsearch_all/RF(%d)_*.final.txt') % val

    for line in fileinput.input(fnames):
        x = count (line,">Myodav*") 
        xa = count (line,">Myotri*")
        xb = count (line,">Tricot*")        



        out.write(val + "\t" + w + "\t" + x + "\t" + wa + "\t" + xa + "\n") #here I think I will have to specified everything individually as a string... 
        print val + "\t" + w + "\t" + x + "\t" + wa + "\t" + xa + "\n"
        val += 1
抱歉,如果我写的东西太奇怪了,如果你知道我在哪里可以读到这个会很高兴知道,谢谢你提前

1 个答案:

答案 0 :(得分:4)

你得到了什么错误?

也许你应该替换

fnames = glob('../cmsearch_all/RF(%d)_*.final.txt') % val

fnames = glob('../cmsearch_all/RF(%d)_*.final.txt' % val)

因为glob()没有返回字符串。