The with open('task_2.json', 'w') as f: closes the file after the indented code runs. This means that you don’t need the f.close() line. Using a with statement in python is just a safer way to work with files.
solved what does with open do in this situation [duplicate]