0

When I run the cell below, and there is no file with the same name as filename, it executes perfectly. But on subsequent runs, if there exists a file with the same name, the kernel gets busy and execution never happens.

I added the flush line, but still no execution.

If I change my filename then again it executes once. After the file is created the cell does not executes.

Below is the code:

import json
import copy

filename = "my_first_match_data.json"
save_data = {'Match': "test", "test2"}
match_data = {'Match': "test", "test2"}
save_data['Match'] = copy.deepcopy(match_data)

try:
    # if I am not appending to an already existing file, it causes some unforeseen error.
    with open(filename, 'w') as json_file:
        json.dump(save_data, json_file)
        json_file.flush()
    print(f"Data successfully saved to {filename}")
except IOError as e:
    print(f"Error saving data to file: {e}")
New contributor
Shubham Patel is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • 1
    Just so you know, JupyterLite is still very experimental. I think though this isn't inherent to that, perhaps. You could check by running it with pyodide-based Python directly; however, that isn't that easy. (I don't think it is that easy. I need to think about this more.) Anyway, the virtual file system Pyodide/JupyterLite use is way more brittle than a real file system and so for overwriting in JupyterLite, you can add deleting the file first if it already exists. That will allow it to work in JupyterLite. Although moot in ipypkernel, additional step will work there, too Commented Nov 18 at 20:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.