-2

Hi sorry this seems to be a very bad question but whenever i create a json txt file in python and write to it, it only saves temporarily. For example, i have a for loop to write text to the file over the amount of times the user wants it to. I know it does it multiple times because i have a counting function to print each time it does it, but the text file that is being written to only saves one loop. So say i want to print the numbers of every time i do the loop, i may ask it to run 4 times, the numbers that should be printed are 0, 1, 2, 3, the only number i see printed to the file at the end is 3 as i keep writing over my data. Here is my code so someone can assist me and like always any and all help is greatly appreciated.

    def create_tasks():
        num_tasks = int(num_tasks_entry.get())
        global size
        num = 0
        for num in range(0, num_tasks):
            data = {}
            data['task'] = []
            data['task'].append({
                'profile': profiles_select.get(),
                'task_id': num,
                'product_link': productlink_entry.get(),
                'delay': int(delay_entry.get()),
            })
            num = num + 1
            with open('tasks.txt', 'w', encoding='utf-8') as outfile:
                json.dump(data, outfile, indent=2)

Another example, in my code i want the out put file to be this information printed the amount of times the user selects.

  • 1
    You’re overwriting the same file in every loop iteration. – AMC Jan 13 '20 at 00:43
  • @AMC yes how can i not do that? –  Jan 13 '20 at 00:44
  • 2
    create empty `data` only once before loop - and write all only once after `for`-loop (not inside `for`-loop). In JSON you can't write many separated dictionares because it will be incorrect JSON data - you have to write all as one dictionary. Or as one list with many dictionaries. – furas Jan 13 '20 at 00:49
  • As an aside, try avoiding globals as much as possible. – AMC Jan 13 '20 at 00:56

1 Answers1

1

You could write your function like this:

    def create_tasks():
        num_tasks = int(num_tasks_entry.get())
        global size
        data = {}
        data['task'] = []
        for num in range(0, num_tasks):
            data['task'].append({
                'profile': profiles_select.get(),
                'task_id': num,
                'product_link': productlink_entry.get(),
                'delay': int(delay_entry.get()),
            })
        with open('tasks.txt', 'w', encoding='utf-8') as outfile:
                json.dump(data, outfile, indent=2)

Also, why are you incrementing num inside the for loop?

Swetank Poddar
  • 1,257
  • 9
  • 23
  • thanks for this. It solved why it was overwriting the data and it was just a simple stupid mistake, lol. One more question for you, how can i save the text in that file so when i close and reopen it doesnt clear the data becuase i am calling a new and fresh file everytime i open it. –  Jan 13 '20 at 01:04
  • @mferraro: I think you are asking for this: https://stackoverflow.com/questions/4706499/how-do-you-append-to-a-file-in-python – Swetank Poddar Jan 13 '20 at 01:11
  • thank you although that is not the answer. As you see everytime i reopen the program and try to write it it clears the file and overwrites it due to me initializing data as {} every time. –  Jan 13 '20 at 01:18
  • 1
    Oh I get it. Then the best way would be to load the file and read the information in data and then write it again... – Swetank Poddar Jan 13 '20 at 01:20
  • hey thanks for that answer before. I was messing around with it and im not getting it done right, is there a refernce you could link to me? Maybe someone who asked this question as well? –  Jan 13 '20 at 05:32
  • here you go https://stackoverflow.com/questions/13949637/how-to-update-json-file-with-python – Swetank Poddar Jan 14 '20 at 17:39