
![]() |
@wtf | |
(Save and load notes) |
||
2
Replies
19
Views
1 Bookmarks
|
![]() |
@wtf | 24 days |
* Reading & Writing Files in Python* *Why Learn File Handling* ? It allows your Python programs to: - Save user data - Read logs or configuration files - Process data stored in .txt, .csv, etc. *Opening a File* file = open(example.txt, r) 'r' = read mode content = file.read() file.close() *Always close the file after opening — or use the safer with keyword* . *Using with (Best Practice)* with open(example.txt, r) as file: content = file.read() print(content) Using with, the file closes automatically after use. *Writing to a File* with open(data.txt, w) as file: file.write(Hello, Python!) - 'w' mode overwrites the file - 'a' mode appends to the file *Reading Line by Line* with open(data.txt, r) as file: for line in file: print(line.strip()) *Mini Project: Notes Saver* filename = notes.txt note = input(Write your note: ) with open(filename, a) as file: file.write(note + n) print(Note saved to, filename) *This tiny app lets the user:* - Write notes - Save them in a .txt file - Append new notes instead of overwriting |
||
![]() |
@sadeeqbigshaq | 22 days |
How to learn it shap
|
||


