简体   繁体   中英

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)

You can use os.listdir() :

base_path = '/home/alispahic/1.CB1_project/12.ProductionRun'
file_name = 'rmsf.xvg'

for dir_name in os.listdir(base_path):
    print(dir_name)
    with open(os.path.join(base_path, dir_name, file_name)) as f:
        for line in f:
            # here goes your code
            pass

Just remember to join the dir_name with the base_path (the path of the directory you are iterating over).

Also note that this returns files as well, not just directories. If you folder /home/alispahic/1.CB1_project/12.ProductionRun contains only directories, then that won't be a problem; otherwise you would need to filter out the files.

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'):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM