31 lines
		
	
	
		
			781 B
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			31 lines
		
	
	
		
			781 B
		
	
	
	
		
			Python
		
	
	
	
	
	
| import json
 | |
| 
 | |
| filepath = "my_data.json"
 | |
| 
 | |
| def write_data(filepath, data, debug=False):
 | |
|   try:
 | |
|     with open(filepath, 'w') as f:
 | |
|       json.dump(data, f, indent=4)  # Use indent for pretty formatting
 | |
|     if debug:
 | |
|         print(f"Data written to '{filepath}'")
 | |
|   except Exception as e:
 | |
|     print(f"Error writing to file: {e}")
 | |
| 
 | |
| 
 | |
| def read_data(filepath, debug=False):
 | |
|   try:
 | |
|     with open(filepath, 'r') as f:
 | |
|       data = json.load(f)
 | |
|     if debug:
 | |
|         print(f"Data read from '{filepath}'")
 | |
|     return data
 | |
|   except FileNotFoundError:
 | |
|     print(f"File not found: {filepath}")
 | |
|     return None
 | |
|   except json.JSONDecodeError:
 | |
|     print(f"Error decoding JSON from file: {filepath}")
 | |
|     return None
 | |
|   except Exception as e:
 | |
|     print(f"Error reading from file: {e}")
 | |
|     return None
 |