🟢 Beginner  ·  Lesson 19

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.

💡 At a Glance
PointDetails
Course AreaCore Python
Basic programming concepts used to write Python programs.
Main Usereading and writing text files
Example Filefile-handling.py
Practice FocusRun, 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.

TermMeaning
open()Function used to open a file for reading or writing.
readGetting data from a file.
writeSaving data into a file.
withContext manager that automatically closes files and resources.
file modesOptions 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.

Basic Pattern
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

Python – file-handling.py
# 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

Python file handling is useful.

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

  1. Type the program in file-handling.py and run it.
  2. Change input values or sample data and observe the new output.
  3. Create one example related to reading and writing text files.
  4. 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

TermMeaning
open()Function used to open a file for reading or writing.
readGetting data from a file.
writeSaving data into a file.
withContext manager that automatically closes files and resources.
file modesOptions 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 करें.

Basic Pattern
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

Python – file-handling.py
# 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

Python file handling is useful.

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

  1. Program ko file-handling.py file me type karke run karein.
  2. Values change karke output compare karein.
  3. reading and writing text files par ek छोटा example banayen.
  4. 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.

← Back to Python Tutorial