File Handling in Python
Python में File Handling
What is File Handling?
File handling lets a program save data to a file and read it back later — so data is not lost when the program ends. This is how real apps store notes, records and reports.
File Modes
| Mode | Meaning |
|---|---|
| "r" | Read (file must exist) |
| "w" | Write (creates new / erases old) |
| "a" | Append (add to the end) |
| "r+" | Read and write |
Open, Read, Close
The basic flow: open() the file, do your work, then close() it. Better still, use with which closes automatically.
Program 1: Write to a File
f = open("notes.txt", "w")
f.write("Hello from Python\n")
f.write("This is line 2\n")
f.close()
print("File written successfully")"w"createsnotes.txt(or erases it if it exists).\nmoves to a new line;.close()saves the file.
Program 2: Read a File
f = open("notes.txt", "r")
content = f.read()
f.close()
print(content).read() returns the whole file as one string.
Program 3: Append Data
f = open("notes.txt", "a")
f.write("This line was added later\n")
f.close()
print("Line appended")"a" keeps the old content and adds the new line at the end (unlike "w" which erases).
Program 4: Read Line by Line with `with`
with open("notes.txt", "r") as f:
for line in f:
print(line.strip())
# file closes automatically herewithcloses the file automatically — even if an error happens.- Looping over
freads one line at a time (memory-friendly). .strip()removes the extra newline.
Common Mistakes
- Using
"w"when you meant"a"— it erases the file! - Forgetting to close the file (use
withto be safe). - Opening a non-existent file in
"r"mode → FileNotFoundError.
Practice Tasks
- Write 5 student names to a file, one per line.
- Read the file and count how many lines it has.
- Append today's date to a log file each time the program runs.
- Read a file and print it in UPPERCASE.
Summary
- File handling saves and reads data permanently.
- Modes:
"r"read,"w"write/erase,"a"append. - Use
with open(...) as f:— it closes the file automatically.
File Handling क्या है?
File handling program को data file में save करके बाद में वापस पढ़ने देता है — ताकि program खत्म होने पर data खो न जाए। असली apps इसी तरह notes, records और reports store करते हैं।
File Modes
| Mode | मतलब |
|---|---|
| "r" | Read (file होनी चाहिए) |
| "w" | Write (नई बनाता / पुरानी मिटाता) |
| "a" | Append (अंत में जोड़ता) |
| "r+" | Read और write |
Open, Read, Close
मूल flow: file open() करें, काम करें, फिर close() करें। और बेहतर, with use करें जो अपने आप close करता है।
Program 1: File में Write करें
f = open("notes.txt", "w")
f.write("Hello from Python\n")
f.write("This is line 2\n")
f.close()
print("File written successfully")"w"notes.txtबनाता है (या मौजूद हो तो मिटा देता है)।\nनई line पर ले जाता है;.close()file save करता है।
Program 2: File Read करें
f = open("notes.txt", "r")
content = f.read()
f.close()
print(content).read() पूरी file को एक string के रूप में लौटाता है।
Program 3: Data Append करें
f = open("notes.txt", "a")
f.write("This line was added later\n")
f.close()
print("Line appended")"a" पुराना content रखकर नई line अंत में जोड़ता है ("w" के उलट जो मिटा देता है)।
Program 4: `with` से Line by Line Read
with open("notes.txt", "r") as f:
for line in f:
print(line.strip())
# file यहाँ अपने आप close होती हैwithfile अपने आप close करता है — error होने पर भी।fपर loop एक बार में एक line पढ़ता है (memory-friendly)।.strip()extra newline हटाता है।
सामान्य गलतियाँ
"a"की जगह"w"use करना — यह file मिटा देता है!- file close करना भूलना (सुरक्षित रहने को
withuse करें)। - न मौजूद file को
"r"mode में खोलना → FileNotFoundError।
Practice Tasks
- 5 student names file में लिखें, हर एक नई line पर।
- File पढ़कर गिनें कि उसमें कितनी lines हैं।
- हर बार program चलने पर log file में आज की date append करें।
- File पढ़कर उसे UPPERCASE में print करें।
सारांश
- File handling data को स्थायी रूप से save और read करता है।
- Modes:
"r"read,"w"write/erase,"a"append। with open(...) as f:use करें — यह file अपने आप close करता है।