File Handling JSON: Concept Notes
"""
Topic 06: File Handling & JSON - Concept Notes
1. Working with Files
- open(filename, mode): Core function to open files.
- Modes: &
- Context Manager(with): Recommended way to open files(automatically closes them).
2. Reading/Writing
- read(), readline(), readlines()
- write(), writelines()
3. JSON Module
- JavaScript Object Notation(JSON) is a lightweight data format.
- json.dump(obj, file): Writes Python object to JSON file.
- json.load(file): Reads JSON file into Python object.
- json.dumps(): Converts object to JSON string.
- json.loads(): Converts JSON string to Python object.
"""
__PH_1__
with open("test.txt", "w") as f:
f.write("Hello, File Handling!\nLine 2\nLine 3")
__PH_2__
with open("test.txt", "r") as f:
content = f.read()
print("File Content:\n", content)
__PH_3__
import json
data = {
"name": "Python Learner",
"topics_covered": 6,
"is_expert": False
}
__PH_4__
with open("data.json", "w") as jf:
json.dump(data, jf, indent=4)
__PH_5__
with open("data.json", "r") as jf:
parsed_data = json.load(jf)
print(f"\nParsed JSON Name: {parsed_data[&