1

I have built a loop in a function for a text file, but the file should take the data from a textfile1 to an textfile2

the textfile 1 is grabbing data and always refreshing initial data

textfile 2 is grabbing data from textfile 1 for new jobs . //: normally textfile1 = textfile 2 (row and data) but more the loop is running and more i have difference between those two file

could you help with my loop issue?

def compteur3():

    evaluation_eleves = []
    infile = open('eleve.txt', 'r+')
    timeout = True
    while not timeout:

        for user in users:
            evaluation_eleves.append([user['id'],user['note'])
            print(evaluation_eleves)

            with open("classe1.txt", "w") as text_file:
                
                for i in evaluation_eleves:
                    print(i, file=text_file)
f = open('classe1', 'r+')
r = open('eleve.txt', 'r+')
text = f.read()
f.seek(0)
r.write(text)
f.truncate()
f.close()
compteur3()
quamrana
  • 37,849
  • 12
  • 53
  • 71
susanoo
  • 11
  • 1
  • When you open a file with mode `w`, you're truncating it -- deleting everything that was there before. You should either open it only _once_ instead of in a loop, or use mode `a` to append. (I'd take the open-only-once approach, myself, putting the entire loop inside the `with` statement, while writing to a temporary file, and rename the temporary file over the real file only when done). – Charles Duffy Aug 10 '21 at 20:31
  • Why do you have: `infile = open('eleve.txt', 'r+')` if you don't use `infile` later? – quamrana Aug 10 '21 at 20:32
  • 1
    What is the actual issue? What happens when you try to run the code, and how is that different from what is supposed to happen? Please be precise. It would help to show an example of `classe1` and `eleve.txt` before and after, and the expected output. – Karl Knechtel Aug 10 '21 at 20:33
  • See [How to make file creation an atomic operation](https://stackoverflow.com/questions/2333872/how-to-make-file-creation-an-atomic-operation) re: the create-and-rename pattern. – Charles Duffy Aug 10 '21 at 20:33
  • When you say "the textfile 1 is grabbing data and always refreshing initial data", do you mean that another program is writing to that file while you are reading it? – Karl Knechtel Aug 10 '21 at 20:34
  • ...if Karl's interpretation above is correct, _how_ the other program is writing to the file matters -- if it's appending, vs rewriting, vs following create-and-rename itself, those all call for different answers on how best to read new content as it becomes available. – Charles Duffy Aug 10 '21 at 20:35
  • users: is a jsonfile and i am taking only data that i want on it. as the values continuously change as the stockmarket , then i need to erase the whole text file with new value in text 1 and do an another operation with numpy for have data rows correctly with textfile 2 – susanoo Aug 10 '21 at 20:43
  • Is a database an option for your use case? – S3DEV Aug 10 '21 at 20:44
  • i am going to test atomic operation cause it seems to be something interesting .i will check tonight . my concerned is "r+" is better for read and write in the same file continuously. i am a newbie but your advise are good . – susanoo Aug 10 '21 at 20:50
  • @S3DEV : database is too complex for me , right now and i have 4 columns and 12 lines, so a text file is simple i think. – susanoo Aug 10 '21 at 20:52

0 Answers0