简体   繁体   中英

How to save the output of two loops in diffrent folders?

I have two loops iterating over files. The loop will output 10 files form each file. i want to save the results in diffrenet folders, for example the first two values for the first loop (1 and 2 ).. i want the file name to be (filename1/2) an saved in a folder called(12) and so for all of the files

A = [1, 3]
B = [2, 4, 6, 8, 10]
for i in A:
    for j in B:
        print(str(i) + " / " + str(j))
        output.append(fun_cal(data, j, i))
np.save(f"../workpass/{Path(filename).stem}.npy", output)

When I save it like this all the 10 values will be saved in one file, but I want to save them in different files in different folders.

Your question is still quite unclear, but the trivial answer is to create a file inside the loop.

import os
...

A = [1, 3]
B = [2, 4, 6, 8, 10]
for i in A:
    for j in B:
        print(str(i) + " / " + str(j))
        output.append(fun_cal(data, j, i))
        # or output = fun_cal(data, j, i)?
        # Create the destination directory if it doesn't exist
        os.makedirs(f"../workpass/{i}{j}", exist_ok=True)
        np.save(f"../workpass/{i}{j}/Path(filename).stem}_{i}_{j}.npy", output)

We obviously have no idea what output is or what it should contain, so you will probably have to rearrange that slightly. I'm guessing np is Numpy but without information about what exactly you want in the file, I did not change that either, just the file name argument.

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