简体   繁体   中英

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

I have this 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.

The problem is that you have not closed the output file (or flushed its contents to disk), so it cannot not be reliably read until it is closed. The solution is to open the file for writing and reading using flag w+ :

with open(input_file, 'r') as source, open(save_file, 'w+') as dest:

And then passing to find_min dest :

find_min(dest, threshold)
# make sure we are once again positioned at the end of file:
# probably not necessary since find_min reads the entire file
# dest.seek(0, 2) 

def find_min(dest, threshold):
    dest.seek(0, 0) # seek to start of file for reading
    reader = csv.reader(dest)
    for i in reader:
        print(i)
    # we should be back at end of file for writing, but if we weren't, then: dest.seek(0, 2)

If find_min does not leave dest positioned at the end of file by not reading through the entire file, then a call to dest.seek(0, 2) must be made before resuming writing to ensure that we are first positioned at the end of file.

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