Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Not Writing in File Python

I have a Threaded class in which I am trying to append data but it is not doing anything, no error no success. The following is the basic structure of code

from threading import Thread

class ABC:
   def func1(self, user):
       # Do processing
       self.func2(lst) # lst Generated in processing

   def func2(self, lst):
        thrd_list = []
        for ele in lst:
            x = Thread(target=self.func3, args(ele, ))
            x.start()
            thrd_list.append(x)
        for thrd in thrd_list:
            thrd.join()

   def func3(self, ele):
       # Do some stuff and if successful write to file
       OUTPUT.write(f"{ele}\n")

with open("users.txt", "r") as f:
     users = f.readlines()
OUTPUT = open("result.txt", "a")
thrd_list = []
for user in users: 
    new_obj = ABC()
    x = Thread(target=new_obj.func1, args(user, ))
    x.start()
    thrd_list.append(x)
for thrd in thrd_list:
    thrd.join()
OUTPUT.close()

The Data is being written on encounter of OUTPUT.close(). I want it to append as it goes so there is no data loss due to crash or bugs

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The data being written to the file is being buffered, and only when the buffer is full or the file is closed is the buffered data written to the file.

Try changing the buffering to be line-buffered:

OUTPUT = open("result.txt", "a", buffering=1)

See https://docs.python.org/3/library/functions.html#open for more details.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading