Important Notice:

writelines() Function (writelines() Method)

writelines() Function (writelines() Method)

3 views 2 min read

writelines() Function (writelines() Method) :-

writelines() Method का उपयोग किसी Iterable जैसे List या Tuple में मौजूद कई Strings को एक साथ File में Write करने के लिए किया जाता है।

English: The writelines() method is used to write multiple strings from an iterable, such as a list or tuple, into a file.

Syntax:

file.writelines(iterable)

यहाँ iterable में एक से अधिक Strings हो सकती हैं।

Example 1: List की सहायता से Multiple Lines लिखना

file = open("demo.txt", "w", encoding="utf-8")

data = [
    "Apple\n",
    "Mango\n",
    "Banana\n",
    "Orange"
]

file.writelines(data)

file.close()

File Content:

Apple
Mango
Banana
Orange

Explanation:

  • data एक List है।

  • List में चार Strings मौजूद हैं।

  • writelines() सभी Strings को File में Write करता है।

  • प्रत्येक String में \n होने के कारण अलग-अलग Lines बनती हैं।

Example 2: Tuple की सहायता से Data लिखना

file = open("demo.txt", "w", encoding="utf-8")

data = ("Red\n", "Green\n", "Blue\n")

file.writelines(data)

file.close()

File Content:

Red
Green
Blue

 

Explanation: writelines() Tuple में मौजूद सभी Strings को File में Write करता है।

Example 3: writelines() में Newline न देना

file = open("demo.txt", "w", encoding="utf-8")

data = ["Hello", "Python", "Programming"]

file.writelines(data)

file.close()

File Content:

HelloPythonProgramming

Explanation:

writelines() Automatically Newline (\n) नहीं जोड़ता। इसलिए सभी Strings एक-दूसरे के तुरंत बाद लिखी जाती हैं।

Example 4: writelines() का Return Value

file = open("demo.txt", "w", encoding="utf-8")

result = file.writelines(["Apple\n", "Mango\n"])

print(result)

file.close()

Output:

None

Explanation:writelines() Method कोई Value Return नहीं करता। इसका Return Value None होता है। Important Points:

  1. writelines() एक साथ कई Strings Write करता है।

  2. इसमें List, Tuple या अन्य Iterable दिया जा सकता है।

  3. writelines() Automatically Newline नहीं जोड़ता।

  4. अलग-अलग Lines के लिए Strings में \n देना आवश्यक है।

  5. इसका Return Value None होता है।

  6. Iterable के Elements Text Mode में Strings होने चाहिए।

Related Notes