Search code examples
pythonpython-3.xloopsdirectoryglob

How do I iterate through folders before the wanted document?


I am trying to code a script that will collect values from a .xvg files. I have 20 folders that contain the targeted file. Folder are numerated from 1-20 (in the code you see 1.Rimo)

I have already made the code that collects the data when I specify full path, however, I need something generic so I can loop through those 20 folders, get that data and store it as a variable.

rmsf = open('/home/alispahic/1.CB1_project/12.ProductionRun/1.Rimo/rmsf.xvg','r+')

for line in rmsf:   
    if line.startswith(' 4755'):
        print (line)
        l = line.split()
        print (l)       
        value = float(l[1])
        sum1 = float(sum1) + value
        print(len(l))
        print (sum1)

Solution

  • I have solved the problem by adding glob.

    for name in glob.glob('/home/alispahic/1.CB1_project/12.ProductionRun/*/rmsf.xvg'):
    
        for line in open(name):
            if line.startswith(' 4755'):