Why is json.dump not writing to my file correctly? or How do I properly format data before using json

16 Replies, 1096 Views

Wait, is your file *actually* empty or just *seems* empty? If you’re opening it in a text editor right after running, some editors (looking at you, VS Code) don’t auto-refresh. Try reopening it manually.

Also, json.dump won’t complain if your data’s valid but your file handle’s borked. Always double-check:
```python
with open('data.json', 'w') as f:
json.dump({"test": 123}, f)
f.flush() # Force write!
```
If that works, your original data’s the culprit.

Messages In This Thread



Users browsing this thread: 1 Guest(s)