File Handling किसी भी type के application का सबसे important part होता है , python में ऐसे कई built in functions defined हैं जिनकी help से file को create , read , write , update , delete किया जा सकता है।

Python Reading File

File Handling के लिए सबसे important function है open() , आपको file read करनी हो या write / update सबसे पहले उस file को open तो करना ही पड़ेगा।


function 2 parameter accept करता है - पहला filename / filepath और दूसरा mode.

Python File open() Syntax

file_p = open(filename, mode)

  1. filename : उस file का name जिसे आपको open करके read करना है , अगर file किसी directory के अंदर है तो उसका full path दिया जाता है।

  2. mode : Generally file को open करने की 4 modes हैं।


    1. "r" : Read - यह default value value है , इस mode से file को सिर्फ read किया ज सकता है। file न मिलने पर error generate होगी।

    2. "a" : Append - यह file में new content append करने के लिए है, अगर file नहीं मिली तो दी गयी location पर file create हो जायगी।

    3. "w" : Write - यह file में file write करने के लिए है, अगर file नहीं मिली तो दी गयी location पर file create हो जायगी।

    4. "x" : Create - यह new file create करने के लिए है , अगर file पहले से exist है तो error generate होगी।


file open हो जाने के बाद read() का use करके आप एक साथ file read कर सकते हैं।

Python File Reading Example

एक file test.txt है जो कि same directory में हैं - जिसमे कुछ इस तरह से content है

File : test.txt

Python :

Known as object oriented, server side language.
Developed By Guido Van Rossum in 1991
Mostly used in data analysis & AI.

अब इस file को read करने का एक program देखेंगे -

Copy Fullscreen Close Fullscreen
f = open('test.txt', "r")
print(f.read())
#close the file.
f.close()
Output
C:\Users\Rahulkumar\Desktop\python>python read_file.py
Python :

Known as object oriented, server side language.
Developed By Guido Van Rossum in 1991
Mostly used in data analysis & AI.

Reading Limited Part

read(500) function एक integer value accept करता है जो बताता है कि file से कितने characters को count करना है।
For Example -

Copy Fullscreen Close Fullscreen
f = open('test.txt', "r")
# getting only 60 characters.
print(f.read(60))
#close the file.
f.close()
Output
C:\Users\Rahulkumar\Desktop\python>python read_file.py
Python :

Known as object oriented, server side language.
De

Reading Line By Line

अगर आप line by line file को read करना चाहते हो तो readline() function का use करके read कर सकते हो।
Example -

Copy Fullscreen Close Fullscreen
f = open('test.txt', "r")
# read first line.
print(f.readline(), end='')
# read second line.
print(f.readline(), end='')
# read third line.
print(f.readline(), end='')
#close the file.
f.close()
Output
C:\Users\Rahulkumar\Desktop\python>python read_file.py
Python :

Known as object oriented, server side language.

Iterating Lines

for loop का use करके file content को iterate भी कर सकते हैं , हर iteration में एक line मिलती है।

Copy Fullscreen Close Fullscreen
f = open('test.txt', "r")
for line in f : print(line, end='')
#close the file.
f.close()
Output
C:\Users\Rahulkumar\Desktop\python>python read_file.py
Python :

Known as object oriented, server side language.
Developed By Guido Van Rossum in 1991
Mostly used in data analysis & AI.

Note : File handling के समय file को close करना कभी न भूले , क्योंकि कभी कभी buffering की वजह से file में हुए changes show नहीं होते हैं अगर file close नहीं हुई तो।

I Hope, अब आपको Python में File Reading अच्छे से समझ आ गया होगा।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers