File Handling in Python
Python में File Handling
What is File Handling in Python?
File Handling in Python means file handling is used to read from and write to files such as text files, logs and reports.
In real programs, this topic helps in reading and writing text files. Learn the idea first, then type the program yourself and compare the output.
| Point | Details |
|---|---|
| Course Area | Core Python Basic programming concepts used to write Python programs. |
| Main Use | reading and writing text files |
| Example File | file-handling.py |
| Practice Focus | Run, change values, and explain the output line by line. |
Why should you learn this?
- It is useful for reading and writing text files.
- It connects with saving reports.
- It improves your ability to read, write and debug Python programs.
Important Terms
These terms are used directly in this lesson. Understand them before memorising the code.
| Term | Meaning |
|---|---|
| open() | Function used to open a file for reading or writing. |
| read | Getting data from a file. |
| write | Saving data into a file. |
| with | Context manager that automatically closes files and resources. |
| file modes | Options like r, w, a that decide how a file is opened. |
Syntax / Basic Pattern
The simple pattern is: prepare data, apply the concept, then show the result.
with open("notes.txt", "w", encoding="utf-8") as file:
file.write("Python file handling is useful.\n")
with open("notes.txt", "r", encoding="utf-8") as file:
data = file.read()
print(data)Complete Example Program
# Writing to a file
with open("notes.txt", "w", encoding="utf-8") as file:
file.write("Python file handling is useful.\n")
# Reading from a file
with open("notes.txt", "r", encoding="utf-8") as file:
data = file.read()
print(data)Expected Output
Program Explanation
with open("notes.txt", "w", encoding="utf-8") as file:stores a value in with open("notes.txt", "w", encoding.file.write("Python file handling is useful.\n")performs the next step of the program logic.with open("notes.txt", "r", encoding="utf-8") as file:stores a value in with open("notes.txt", "r", encoding.data = file.read()stores a value in data.print(data)displays information or calculated result on the screen.
Where will you use it?
- Reading and writing text files.
- Saving reports.
- Creating simple storage.
Common Mistakes
- Writing code with wrong indentation.
- Using input() value directly in calculations without converting it to int or float.
- Using unclear variable names that make the program difficult to understand.
Practice Tasks
- Type the program in
file-handling.pyand run it. - Change input values or sample data and observe the new output.
- Create one example related to reading and writing text files.
- Write 5 lines explaining the logic in your own words.
Summary
File Handling in Python is not a theory-only topic. You should be able to explain the meaning, write the example, run it successfully, and use it in a small practical program.
Python File Handling क्या है?
Python File Handling ka matlab hai: File handling is used to read from and write to files such as text files, logs and reports. Simple words me, ye topic practical Python programs likhne me direct use hota hai.
Is topic ko sirf definition ke liye nahi, balki reading and writing text files jaise real examples ke liye practice karein.
यह क्यों सीखना जरूरी है?
- Ye reading and writing text files me kaam aata hai.
- Ye saving reports se bhi connected hai.
- Isse aap code ka output aur errors better samajh paate hain.
Important Terms
| Term | Meaning |
|---|---|
| open() | Function used to open a file for reading or writing. |
| read | Getting data from a file. |
| write | Saving data into a file. |
| with | Context manager that automatically closes files and resources. |
| file modes | Options like r, w, a that decide how a file is opened. |
Syntax / Basic Pattern
Basic idea: pehle data तैयार करें, phir Python logic apply करें, aur finally result display करें.
with open("notes.txt", "w", encoding="utf-8") as file:
file.write("Python file handling is useful.\n")
with open("notes.txt", "r", encoding="utf-8") as file:
data = file.read()
print(data)Complete Example Program
# Writing to a file
with open("notes.txt", "w", encoding="utf-8") as file:
file.write("Python file handling is useful.\n")
# Reading from a file
with open("notes.txt", "r", encoding="utf-8") as file:
data = file.read()
print(data)Expected Output
Program Explanation
with open("notes.txt", "w", encoding="utf-8") as file:stores a value in with open("notes.txt", "w", encoding.file.write("Python file handling is useful.\n")performs the next step of the program logic.with open("notes.txt", "r", encoding="utf-8") as file:stores a value in with open("notes.txt", "r", encoding.data = file.read()stores a value in data.print(data)displays information or calculated result on the screen.
Practical Uses
- Reading and writing text files.
- Saving reports.
- Creating simple storage.
Common Mistakes
- Writing code with wrong indentation.
- Using input() value directly in calculations without converting it to int or float.
- Using unclear variable names that make the program difficult to understand.
Practice Tasks
- Program ko
file-handling.pyfile me type karke run karein. - Values change karke output compare karein.
- reading and writing text files par ek छोटा example banayen.
- Logic ko apne words me 5 lines me likhein.
सारांश
File Handling in Python ko tab complete maanenge jab aap iska meaning, example, output aur practical use clearly explain kar saken.