Seedha content par jao
🤖 Python Hindi Bot
Namaste! Main Python Hindi Bot hoon 🐍
Python ya AI se related kuch bhi type karke pucho, ya neeche se quick sawaal chuno.
WhatsApp

Stay Connected with PythonHindii

Join our WhatsApp Community to get the latest Python tutorials, coding tips, interview questions, project updates, and learning resources directly on your phone.

Join WhatsApp Community
⇧
Python Hindi Logo
Python in Hindi | Programming Education
🎓 100% Free · Hindi me · Beginner se Career tak

Python Tutorial in HindiiLearn Python Programming for Free

Python, AI, Data Analytics aur SQL ek hi jagah, simple Hindi me. Padho, browser me code chalao, projects banao aur career shuru karo.

main.py
# Python seekho, Hindi me naam = "Ravi" for i in range(3): print("Namaste", naam)
▸ python main.py
🐍 Python 🤖 AI & ML 📊 Data 🗄️ SQL

Chahe Student ho ya Professional, yahan aapke liye sab hai

Apna card chuno, seedha sahi jagah pahunch jaoge.

🎓

Student ho?

School ya college me ho? Python zero se seekho, notes aur cheat sheets ke saath.

  • Python basics se OOP tak
  • Notes, PDFs aur Cheat Sheets
  • Browser me practice, kuch install nahi
💼

Working Professional ho?

Kaam ke saath nayi skill jodo: data, SQL aur AI se apna kaam tez karo.

  • Pandas, NumPy, Excel, Power BI
  • SQL aur MySQL databases
  • Automation aur AI Tools
🚀

Job ya Career change?

Projects banao, interview ki taiyari karo aur sahi roadmap follow karo.

  • Real projects, portfolio ke liye
  • Python aur SQL interview questions
  • AI Engineer, Data Scientist roadmap

Python Hindii Kaise Kaam Karta Hai?

Ek hi jagah 4 layers: seekho, practice karo, skills banao aur career shuru karo. Har layer ek step hai, us par click karke seedha wahan pahuncho. Computer par drag karke 3D me ghumao bhi.

Layer 1 · Seekho

Python Basics se Advanced tak

Variables, loops, functions se OOP tak, sab simple Hindi me. Yahin se shuru karo.

Naye ho? 3 step me shuru karo

💻 Online Code Editor

Browser me hi code likho aur turant chalao

Koi software install nahi. Code likho, Run dabao aur result usi waqt dekho.

  • Python, Java, C, C++, JavaScript, HTML aur aur bhi languages
  • input(), graphs aur multi-file HTML project bhi chalte hain
  • Mobile aur computer, dono par chalta hai
main.py
naam = input("Naam batao: ") for i in range(3): print("Namaste", naam)
▸ python main.py
PythonJavaCC++JavaScriptHTML

Python se AI tak ka poora rasta

8 steps, ek ke baad ek. Aage-peeche ghumao aur apni stage se seedha shuru karo.

Step 1 🐍

Python Basics

IntroductionVariablesData TypesOperatorsInput & OutputConditionsLoops
Start Python →
Step 2 💻

Python Intermediate

ListsTuplesSetsDictionariesStringsModulesFile Handling
Continue Python →
Step 4 🗄️

SQL & Databases

SQL BasicsMySQLSELECTINSERTUPDATEDELETEJOIN
Learn SQL →
Step 5 📊

Data Analytics

NumPyPandasData CleaningData VisualizationMatplotlibExploratory Data Analysis
Learn Data Analytics →
Step 6 🤖

Machine Learning

ML BasicsSupervised & Unsupervised LearningRegressionClassificationModel EvaluationScikit-learn
Learn Machine Learning →
Step 7 🧠

Artificial Intelligence

AI FundamentalsGenerative AIAI ToolsPrompt EngineeringLLM ConceptsAI Automation
Explore AI →
Step 8 🚀

Projects & Career

Python ProjectsAI ProjectsData ProjectsPortfolioGitHubInterview PreparationCareer Roadmap
Build Your Career →
Step 1 / 8

Python Me File Kaise Read Kare? Complete Guide in Hindi

Why is file reading used in Python - Python File Reading

Python File Reading

अब तक हमारे programs का सारा data सिर्फ program चलने तक ही रहता था — program बंद, और data गायब। Real projects में data अक्सर files में रखा होता है — जैसे notes, student list, या log files। इस lesson में हम सीखेंगे कि Python से किसी file का data कैसे पढ़ा (read) जाता है।

अगर आपने File Handling का overall idea अभी नहीं देखा है, तो पहले Python File Handling in Hindi: Core Concepts & Workflow पढ़ लें।

📌 इस Lesson का Scope: इस lesson में केवल Read Files पर focus है। File Writing, Append, CSV, JSON, Binary Files और File Handling के दूसरे topics को यहाँ detail में नहीं पढ़ाया गया है।

Practice के लिए File तैयार करें

File reading सीखने के लिए हमें एक असली file चाहिए। अपने computer में notes.txt नाम की एक text file बनाइए और उसमें ये 3 lines लिखिए। फिर अपनी Python file (.py) को उसी folder में रखिए।

📄 notes.txt
Python is easy File handling is useful Reading files is fun
Note: आखिरी line के बाद Enter मत दबाइए — यानी तीसरी line के बाद कोई खाली line न हो। साथ ही, इस lesson के examples को VS Code या अपने computer के terminal में चलाइए, क्योंकि ये examples एक real file पर निर्भर हैं जो Online Editor में मौजूद नहीं होगी।

File से Data Read करना

File से data पढ़ने का मतलब है — file में लिखी जानकारी को Python program के अंदर लाना, ताकि हम उसे print कर सकें, process कर सकें, या उस पर कोई काम कर सकें।

इसके लिए हमेशा यह क्रम चलता है:

File खोलो → Data पढ़ो → File बंद करो

open() का Read Purpose

किसी भी file को खोलने के लिए Python का built-in function open() इस्तेमाल होता है। Read करने के लिए file को "r" (read) mode में खोला जाता है।

Syntax
file_object = open("file_name", "r")
  • "file_name" → जिस file को खोलना है उसका नाम (और जरूरत हो तो path)
  • "r" → read mode, यानी file को सिर्फ पढ़ने के लिए खोलना
Note: "r" Python का default mode है, इसलिए open("notes.txt") लिखना भी open("notes.txt", "r") जैसा ही है। लेकिन शुरुआत में "r" साफ-साफ लिखने से code का मतलब ज्यादा clear रहता है।

File Object का Basic Concept

open() किसी file का content सीधे नहीं देता — वह एक file object return करता है। यह file object उस file से जुड़ा एक "रास्ता" है, जिसके जरिए हम बाद में read(), readline() जैसे methods से data पढ़ते हैं।

Example
f = open("notes.txt", "r") print(type(f)) f.close()

Output:

<class '_io.TextIOWrapper'>

यहाँ f में file का content नहीं, बल्कि file object है। काम खत्म होने पर f.close() से file को बंद कर देना चाहिए।

🎥 Video में पूरा Topic समझें

read() Method

read() file का content एक string के रूप में वापस देता है।

पूरी File का Content पढ़ना

Example
f = open("notes.txt", "r") content = f.read() print(content) f.close()

Output:

Python is easy File handling is useful Reading files is fun

बिना कोई number दिए read() लिखने पर पूरी file एक ही बार में, एक string में आ जाती है।

read(size) से Limited Characters पढ़ना

read() में एक number देने पर वह उतने ही characters पढ़ता है।

Example
f = open("notes.txt", "r") print(f.read(10)) print(f.read(4)) f.close()

Output:

Python is easy

पहली बार में 10 characters मिले ("Python is " — is के बाद एक space भी है)। दूसरी बार में पिछली जगह से आगे के 4 characters ("easy") मिले — यानी read(size) हर बार वहीं से आगे बढ़ता है जहाँ पिछली बार रुका था।

readline() से एक-एक Line पढ़ना

readline() हर call में file की सिर्फ एक line पढ़ता है, और अगली call में अगली line।

Example
f = open("notes.txt", "r") print(f.readline()) print(f.readline()) f.close()

Output:

Python is easy File handling is useful

दोनों lines के बीच एक खाली line क्यों आई? क्योंकि file की हर line के आखिर में एक छिपा हुआ new line character (\n) होता है, और print() अपनी तरफ से भी एक new line जोड़ देता है — यानी दो बार new line।

Example — खाली line हटाने के दो तरीके
f = open("notes.txt", "r") # तरीका 1: print में end="" लगाना print(f.readline(), end="") # तरीका 2: strip() से \n हटाना line2 = f.readline().strip() print(line2) f.close()

Output:

Python is easy File handling is useful

Strings के strip() जैसे methods के बारे में और जानने के लिए Python Strings in Hindi पढ़ सकते हैं।

readlines() से सभी Lines पढ़ना

readlines() पूरी file की सारी lines को एक list में वापस देता है — हर line उस list का एक element होती है।

Example
f = open("notes.txt", "r") lines = f.readlines() print(lines) print(len(lines)) f.close() for line in lines: print(line.strip())

Output:

['Python is easy\n', 'File handling is useful\n', 'Reading files is fun'] 3 Python is easy File handling is useful Reading files is fun

ध्यान दें — पहली दो lines के आखिर में \n मौजूद है, जबकि आखिरी line में नहीं (क्योंकि हमने उसके बाद Enter नहीं दबाया था)। Lists के बारे में revise करने के लिए Python Lists Complete Guide देख सकते हैं।

read(), readline() और readlines() में Difference

Methodक्या return करता हैकब इस्तेमाल करें
read()पूरी file एक string मेंछोटी file का पूरा text चाहिए हो
readline()एक बार में एक line (string)Lines को एक-एक करके, अपने control में पढ़ना हो
readlines()सारी lines की listLines की गिनती करनी हो, या list की तरह loop चलाना हो

हमारी notes.txt पर तीनों का result एक नज़र में:

read() → 'Python is easy\nFile handling is useful\nReading files is fun' readline() → 'Python is easy\n' readlines() → ['Python is easy\n', 'File handling is useful\n', 'Reading files is fun']

File Pointer / Cursor का Basic Concept

File पढ़ते समय Python एक अदृश्य cursor (file pointer) रखता है, जो बताता है कि अभी file में कहाँ तक पढ़ा जा चुका है। हर बार पढ़ने पर यह cursor उतना आगे बढ़ जाता है।

Python is easy\nFile handling is useful\nReading files is fun ^ cursor शुरुआत में (position 0)

cursor की मौजूदा position देखने के लिए tell() इस्तेमाल किया जा सकता है।

Example
f = open("notes.txt", "r") print(f.tell()) f.read(10) print(f.tell()) f.read(4) print(f.tell()) f.close()

Output:

0 10 14

शुरू में cursor 0 पर था। 10 characters पढ़ने के बाद 10 पर, और 4 और पढ़ने के बाद 14 पर पहुँच गया।

दोबारा read() करने पर क्या होता है

जब read() पूरी file पढ़ लेता है, तो cursor file के आखिर में पहुँच जाता है। अब दोबारा read() करने पर पढ़ने के लिए कुछ बचा ही नहीं होता, इसलिए Python एक खाली string देता है।

Example
f = open("notes.txt", "r") first = f.read() second = f.read() print(first) print(repr(second)) f.close()

Output:

Python is easy File handling is useful Reading files is fun ''

दूसरी बार का result '' यानी बिल्कुल खाली string है — कोई error नहीं आई, बस पढ़ने को कुछ बचा ही नहीं था।

seek() का Basic Use

seek() cursor को file में किसी भी position पर वापस ले जाता है। सबसे common इस्तेमाल है seek(0) — यानी cursor को file की शुरुआत में वापस ले आना, ताकि file दोबारा पढ़ी जा सके।

Example — seek(0) से दोबारा पढ़ना
f = open("notes.txt", "r") first = f.read() f.seek(0) second = f.read() print(first == second) f.close()

Output:

True

seek(0) ने cursor को शुरुआत में भेज दिया, इसलिए दूसरी बार भी पूरा content मिला और दोनों बार का result बिल्कुल same रहा।

Example — किसी खास position पर जाना
f = open("notes.txt", "r") f.seek(7) print(f.read(7)) f.close()

Output:

is easy

seek(7) ने cursor को 7वीं position पर रखा (जहाँ "is easy" शुरू होता है), और वहीं से 7 characters पढ़े गए।

Note: Text files में seek(0) सबसे सुरक्षित और सबसे ज्यादा इस्तेमाल होने वाला तरीका है। ऊपर seek(7) वाला example एक साधारण English (ASCII) text file पर है — बाकी characters वाली files में अलग-अलग positions पर जाना हमेशा इतना सीधा नहीं होता।

with open() के साथ File Read करना

हर बार f.close() लिखना भूल जाना आसान है। with open() इस समस्या को हल करता है — यह काम खत्म होते ही file को अपने-आप बंद कर देता है, चाहे बीच में कोई error ही क्यों न आ जाए।

Syntax
with open("file_name", "r") as file_object: # file पर काम यहाँ करें
Example
with open("notes.txt", "r") as f: content = f.read() print(content) print(f.closed)

Output:

Python is easy File handling is useful Reading files is fun True

f.closed का True आना दिखाता है कि with block खत्म होते ही file अपने-आप बंद हो चुकी है — हमें close() लिखना ही नहीं पड़ा।

Tip: आगे से file पढ़ने का default तरीका with open() ही रखिए। यह ज्यादा सुरक्षित और साफ-सुथरा है।

Real-World Text File Reading Example

मान लीजिए आपके पास एक students.txt file है, जिसमें हर line में एक student का नाम लिखा है:

📄 students.txt
Ankit Priya Rahul Sneha
Example — Student List पढ़ना
with open("students.txt", "r") as f: students = f.readlines() print("Total students:", len(students)) for student in students: print("-", student.strip())

Output:

Total students: 4 - Ankit - Priya - Rahul - Sneha

readlines() से सारे नाम एक list में आ गए, len() से उनकी गिनती निकली, और strip() से हर नाम के साथ चिपका \n हटाया गया।

Example — File में कोई Word ढूँढना
with open("notes.txt", "r") as f: text = f.read() word = "handling" if word in text: print("Word mil gaya:", word) else: print("Word nahi mila")

Output:

Word mil gaya: handling

Common Mistakes

❌ File का नाम या Path गलत देना
f = open("nofile.txt", "r")

Output:

FileNotFoundError: [Errno 2] No such file or directory: 'nofile.txt'

Python को वह file उस folder में मिली ही नहीं जहाँ से आपका program चल रहा है।

✅ सही तरीका

File का नाम (और extension, जैसे .txt) ध्यान से जाँचिए, और पक्का कीजिए कि file आपकी .py file वाले folder में ही है। गलती संभालने का तरीका Python Exception Handling in Hindi में सीख सकते हैं।

❌ with block के बाहर file पढ़ने की कोशिश करना
with open("notes.txt", "r") as f: pass print(f.read())

Output:

ValueError: I/O operation on closed file.

with block खत्म होते ही file बंद हो जाती है, इसलिए उसके बाद पढ़ना possible नहीं।

✅ सही तरीका — पढ़ना with block के अंदर ही करें
with open("notes.txt", "r") as f: content = f.read() print(content)
❌ दोबारा read() करने पर content की उम्मीद करना
content1 = f.read() content2 = f.read() # यह खाली string होगी

पहली read() के बाद cursor आखिर में पहुँच चुका होता है, इसलिए दूसरी बार कुछ नहीं मिलता।

✅ सही तरीका
content1 = f.read() f.seek(0) content2 = f.read()
❌ readlines() के result को string समझना
lines = f.readlines() print(lines.upper()) # AttributeError

Output:

AttributeError: 'list' object has no attribute 'upper'

readlines() एक list देता है, string नहीं — string वाले methods इस पर सीधे नहीं चलते।

✅ सही तरीका — हर line पर अलग से काम करें
for line in lines: print(line.strip().upper())
❌ close() लिखना भूल जाना

open() के बाद close() न लिखने से file खुली रह जाती है। इसीलिए with open() का इस्तेमाल करना बेहतर है, जो file को अपने-आप बंद कर देता है।

Hindi text वाली file के लिए: अगर file में Hindi (या दूसरी भाषाओं का) text है और पढ़ते समय UnicodeDecodeError आए, तो file को open("file.txt", "r", encoding="utf-8") लिखकर खोलिए। कई systems (खासकर Windows) पर default encoding UTF-8 नहीं होती।

Exercise ?

readlines() method क्या return करता है?

पूरी file एक string में
सारी lines की एक list
सिर्फ पहली line

Final Coding Challenge

अपने computer में my_notes.txt नाम की एक text file बनाइए जिसमें अपनी पसंद की 5 lines हों (हर line अलग)। फिर VS Code में इन tasks को पूरा कीजिए:

  1. with open() और read() से पूरी file का content print कीजिए।
  2. read(15) से सिर्फ पहले 15 characters print कीजिए।
  3. readline() को तीन बार call करके पहली तीन lines print कीजिए (खाली line न आए — strip() या end="" इस्तेमाल करें)।
  4. readlines() से सारी lines की list बनाइए और len() से बताइए कि file में कुल कितनी lines हैं।
  5. एक loop चलाकर हर line को line number के साथ print कीजिए, जैसे 1. पहली line।
  6. एक ही file object पर read() दो बार call करके देखिए कि दूसरी बार क्या मिलता है, फिर seek(0) लगाकर उसे ठीक कीजिए।
  7. Bonus: किसी ऐसी file का नाम देकर देखिए जो मौजूद ही नहीं है, और कौन सी error आती है यह नोट कीजिए।

Quick Revision

ConceptSyntax / मतलब
File खोलना (read)open("file.txt", "r")
File Objectopen() जो वापस देता है, content नहीं
पूरी file पढ़नाf.read()
कुछ characters पढ़नाf.read(10)
एक line पढ़नाf.readline()
सारी lines (list)f.readlines()
Cursor की positionf.tell()
Cursor को शुरुआत में लानाf.seek(0)
अपने-आप बंद होने वाला तरीकाwith open("file.txt", "r") as f:

FAQ

Python में file को पढ़ने के लिए कौन सा mode इस्तेमाल होता है?

Read mode "r" इस्तेमाल होता है। यह Python का default mode भी है, इसलिए open("notes.txt") और open("notes.txt", "r") एक जैसे ही काम करते हैं।

read(), readline() और readlines() में क्या फर्क है?

read() पूरी file एक string में देता है, readline() एक बार में सिर्फ एक line देता है, और readlines() सारी lines की एक list देता है।

दोबारा read() करने पर खाली string क्यों मिलती है?

क्योंकि पहली बार पूरी file पढ़ने के बाद file pointer (cursor) file के आखिर में पहुँच जाता है। अब आगे पढ़ने को कुछ बचा नहीं होता। seek(0) से cursor को शुरुआत में लाकर फिर से पढ़ा जा सकता है।

with open() का इस्तेमाल क्यों बेहतर माना जाता है?

क्योंकि यह काम खत्म होते ही file को अपने-आप बंद कर देता है, चाहे बीच में कोई error ही क्यों न आए। इससे close() लिखना भूलने की गलती नहीं होती।

पढ़ी गई lines में \n क्यों दिखता है?

File की हर line के आखिर में एक new line character (\n) होता है, और readline()/readlines() उसे भी साथ में पढ़ते हैं। उसे हटाने के लिए strip() इस्तेमाल किया जाता है।

FileNotFoundError क्यों आती है?

जब Python को दिए गए नाम की file उस folder में नहीं मिलती जहाँ से program चल रहा है — यानी file का नाम, extension या location गलत होती है।

Summary

File Reading = file खोलो → read(), readline() या readlines() से data पढ़ो → file बंद करो (या with open() से अपने-आप बंद होने दो)।

इस lesson में आपने सीखा कि open() read mode में कैसे काम करता है, file object क्या होता है, read(), read(size), readline() और readlines() में क्या फर्क है, file pointer (cursor) कैसे आगे बढ़ता है, दोबारा read() करने पर खाली string क्यों मिलती है, seek() से cursor को वापस कैसे लाते हैं, और with open() से file को सुरक्षित तरीके से कैसे पढ़ते हैं।

File Reading पक्की हो जाए तो आगे के File Handling topics सीखना बहुत आसान हो जाता है। अब ऊपर दिया गया coding challenge खुद VS Code में solve कीजिए।

Related Articles

🧑‍💻 Python Coding Lab

Python code likho, run karo aur practice karo — directly browser mein.