Skip to content
Advertisement

iterating over folders executing a fuction at each 2 folders

I have a function called plot_ih_il that receives two data frames in order to generate a plot. I also have a set of folders that each contain a .h5 file with the data I need to give to the function plot_ih_il… I’m trying to feed the function two datasets at a time but unsuccessfully.

I’ve been using pathlib to do so

path = Path("files")

 for log in path.glob("log*"):
     for file in log.glob("log*.h5"):
       df = pd.DataFrame(file, key = "log")

but using this loop, I can only feed one data frame at a time, I need two of them.

The structure of the folders is something like,

files->log1-> log1.h5
       log2-> log2.h5
       log3-> log3.h5
       log4-> log4.h5

I would like to feed the function plot_il_ih the following sequence,

plot_il_ih(dataframeof_log1.h5, dataframeof_log2.h5) then
plot_il_ih(dataframeof_log2.h5, dataframeof_log3.h5) and so on.

I have tried to use zip

def pairwise(iterable):
    
    a = iter(iterable)
    return zip(a, a)

for l1, l2 in pairwise(list(path.glob('log*'))):
    plot_il_ih(l1, l2)

but it doesn’t move forward, just opens the 2 firsts.

What is wrong with my logic?

Advertisement

Answer

consider something like this. You might have to play around with the indexing

filelist = list(path.glob('log*'))
for i in range(1, len(filelist)):
    print(filelist[i-1])
    print(filelist[i])
    print('n')

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement