Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to iterate in a file inside a function in Python3?

I have this python function where I open two files, one for reading and one for writing. I iterate through input_file and I write some items from it to the save_file.

with open(input_file, 'r') as source, open(save_file, 'w') as dest:
        reader = csv.reader(source)
        writer = csv.writer(dest)
        
        for row in reader:
            #do_something

            find_min(save_file, threshold)
                

Although through the iteration I want to call another function and iterate through the items that I have appended on the save_file, but when i call it and try to print them nothing is printed.

This is the function that I call:

def find_min(file, threshold):

    with open(file, 'r') as f:
        reader = csv.reader(f)
        for i in reader:
            print(i)

If I try to call find_min function outside of with statement, the file is iterated normally and it's printed.

But I want to call this function a lot of times in order to analyze and compress the initial data.

So, does anyone know how to to iterate through the save_file in find_min function.

Sign In or Register to comment.