How to readlines in Python

How to readlines in Python

In Python, the readlines() method returns a list containing each line in the file as a list item. Use the hint parameter to limit the number of lines returned. If the total number of bytes returned exceeds the specified number, no more lines are returned.

पायथन में, रीडलाइन() मेथड सूची में आइटम के रूप में फाइल में प्रत्येक पंक्ति वाली सूची रिटर्न करती है। दिए गए लाइनों की संख्या को सीमित करने के लिए संकेत पैरामीटर का उपयोग करें। यदि दिए गए बाइट्स की कुल संख्या निर्दिष्ट संख्या से अधिक है, तो अधिक लाइनें रिटर्न नहीं आती हैं।

Syntax: fileObject.readlines (sizehint)

sizehint – Optional. If the number of bytes returned exceeds the hint number, no more lines will be returned. Default value is -1, which means all lines will be returned.

साईजहिन्ट- ऑप्शनल । यदि बाइट्स की संख्या संकेत संख्या से अधिक हो जाती है, तो कोई और रेखा वापस नहीं आएगी। डिफॉल्ट मान -1 है, जिसका अर्थ है कि सभी लाइनें वापस आ जाएंगी।

Return Value – this method returns a list containing the lines.

रिटर्न वैल्यू- यह विधि एक सूची देती है जिसमें रेखाएँ होती हैं।

Example:
fileObject = open(“gyancs.txt”, “r”)
print (fileObject.readlines () )

Output:
[‘Be a Master\n’, ‘Be a Creator\n’, “Don’t follow Anyone, Learn from
everyone\n”]

write()

The write() method writes a specified text to the file. Where the specified text will be inserted depends on the file mode and stream position.

राईट() मेथड फाइल में एक निर्दिष्ट टेक्स्ट राईट करती है। जहां निर्दिष्ट टेक्स्ट डाला जाएगा वह फाइल मोड और स्ट्रीम स्थिति पर निर्भर करता है।

  • “a”: The text will be inserted at the current file stream position, default at the end of the file.

टेक्स्ट को वर्तमान फाइल स्ट्रीम स्थिति में डाला जाएगा, फाइल के अंत में डिफॉल्ट ।

  • “w”: The file will be emptied before the text will be inserted at the current file stream position, default 0.

फाइल को वर्तमान फाइल स्ट्रीम स्थिति, डिफॉल्ट 0 पर सम्मिलित करने से पहले फाइल खाली कर दी जाएगी।

Syntax: fileObject.write (text [by default] | byte)

Example:
fileObject = open (“gyancs.txt”, “a”)
fileObject.write(“\nTake courage again and again”)
fileObject.close()
# check whether appended or not
fileObject = open(“gyancs.txt”, “r”)
print (fileObject.read())
fileObject.close()

Output:
Be a Master
Be a Creator
Don’t follow Anyone, Learn from everyone
Take courage again and again

writelines()

The writelines() method writes the items of a list to the file. Where the texts will be inserted depends on the file mode and stream position.

राइटलाइन() मेथड किसी सूची के आइटम को फाइल में राईट करती है। जहां टेक्स्ट्स को डाला जाएगा, वह फाइल मोड और स्ट्रीम स्थिति पर निर्भर करता है।

  • “a”: The texts will be inserted at the current file stream position, default at the end of the file.

टेक्स्ट्स को वर्तमान फाइल स्ट्रीम स्थिति में डाला जाएगा, फाइल के अंत में डिफॉल्ट।

  • “w”: The file will be emptied before the texts will be inserted at the current file stream position, default 0.

फाइल को वर्तमान फाइल स्ट्रीम स्थिति में डाला जाएगा, डिफॉल्ट 0 से पहले फाइल खाली कर दी जाएगी।

Syntax: fileObject.writelines (list)

list – The list of texts or byte objects that will be inserted.

लिस्ट– उन टेक्स्ट्स या बाइट ऑब्जेक्ट्स की सूची, जिन्हें सम्मिलित किया जाएगा।

Example: To write to a file, using writelines() function

उदाहरण- राईटलाईन) फंक्शन का इस्तेमाल करके एक फाइल राईट करना।

fileObject = open(“python.txt”, “w”)
lines_of_text = [“a line of text”, “another line of text”, “a third line”]
fileObject.writelines (lines_of_text)
fileObject.close()

Note: In the above program, data present in the file will be overridden everytime if we re-run the program. Instead of overriding if we want append operation then we should open the file as follows.

नोट– उपरोक्त प्रोग्राम में, फाइल में मौजूद डेटा हर बार ओवरराइड किया जाएगा यदि हम प्रोग्राम को फिर से शुरू करते हैं। ओवरराइड करने के बजाय अगर हम एपेंड ऑपरेशन चाहते हैं तो हमें फाइल को निम्न प्रकार से ओपेन करना चाहिए।

fileObject = open (“python.txt”, “a”)

Example:
fileObject = open(“python.txt”, “a”)
lines_of_text = [“a line of text\n”, “another line of text\n”, “a third line”]
fileObject.writelines (lines of text)
fileObject.close()
fileObject = open(“python.txt”, ‘r’)
data = fileObject.read()
print (data)
fileObject.close()

Output:
a line of text
another line of text
a third line

Note: While writing data by using write() methods, compulsory we have to provide line separator ‘\n’, otherwise total data should be written to a single line.

नोट– राईट() मेथड का उपयोग करके डेटा राईट करते समय, हमें अनिवार्य रूप से लाइन सेपरेटर ‘n’ प्रदान करना होगा, | अन्यथा कुल डेटा को एक लाइन में लिखा जाना चाहिए।

tell()

The tell() method returns the current file position in a file stream. You can change the current file position with the seek() method.

टेल() मेथड एक फाइल स्ट्रीम में वर्तमान फाइल स्थिति रिटर्न करती है। आप वर्तमान फाइल स्थिति को सीक) मेथड से बदल सकते हैं।

Syntax: fileObject.tell()

Example:
fileObject = open(“gyancs.txt”, “r”)
print(“File position :”, fileObject.tell())
print (“1st Line :”, fileObject.readline ())
print(“File position :”, fileObject.tell())
print (“2nd Line :”, fileobject.readline ())
print (“File position : “, fileObject.tel1 ())
fileObject.close()

Output:
File position : 0
1st Line : Be a Master
File position : 13
2nd Line : Be a Creator
File position : 27

seek()

The seek() method sets the current file position in a file stream. The seek() method also returns the new position.

सीक() मेथड एक फाइल स्ट्रीम में वर्तमान फाइल स्थिति सेट करती है। सीक) मेथड भी नया पोजीशन रिटर्न करती है।

Syntax: fileObject.seek (offset)

Offset– Required. A number representing the position to set the current file stream position.

ऑफसेट– आवश्यक है। वर्तमान फाइल स्ट्रीम स्थिति सेट करने के लिए स्थिति का प्रतिनिधित्व करने वाला एक नंबर ।

Example:
fileobject = open (“gyancs.txt”, “r”)
print(“File position :”, fileObject.tell())
print (fileObject.readline () )
fileObject.seek (6)
print (fileObject.readline () )
print (“File position : “, fileObject.seek (6)

Output:
File position : 0
Be a Master
Master
File position : 6

Chapter wise Model Paper Link

About Me