Write Multiple Lines to a File in Python

To write multiple lines to a file in Python, you can use the write() method in a loop, the writelines() method for writing a list of strings, or use context managers (with open()) to handle files efficiently. In this tutorial, we will explore different approaches with examples.


Examples

1. Write Multiple Lines to a File Using write() in a Loop

We can write multiple lines to a file using the write() method inside a loop.

main.py

</>
Copy
# Open the file in write mode
with open("output.txt", "w") as file:
    lines = ["Line 1\n", "Line 2\n", "Line 3\n"]  # List of lines

    # Writing lines one by one
    for line in lines:
        file.write(line)

print("File written successfully!")

Explanation:

  1. We open a file named output.txt in write mode ("w").
  2. The variable lines is a list of strings, where each string represents a line of text followed by a newline character (\n).
  3. Using a for loop, we write each line to the file using the write() method.
  4. The with open() statement ensures the file is closed automatically after writing.

Output

Content of output.txt:

2. Write Multiple Lines to a File Using writelines() Method

The writelines() method writes multiple lines from a list directly to a file without looping.

main.py

</>
Copy
# Open the file in write mode
with open("output.txt", "w") as file:
    lines = ["Hello World!\n", "Python is great!\n", "File handling is easy.\n"]

    # Writing all lines at once
    file.writelines(lines)

print("File written successfully!")

Explanation:

  1. The file output.txt is opened in write mode.
  2. The lines variable is a list of strings, each ending with a newline character (\n).
  3. Instead of using a loop, we pass the entire list to the writelines() method, which writes all lines at once.

Output (Content of output.txt):

3. Writing to a File Using join() Method

We can use the join() method to create a single string before writing it to a file.

main.py

</>
Copy
# Open the file in write mode
with open("output.txt", "w") as file:
    lines = ["apple\n", "banana\n", "mango\n"]

    # Writing the joined string
    file.write("".join(lines))

print("File written successfully!")

Explanation:

  1. The file output.txt is opened in write mode.
  2. We have a list of lines stored in the variable lines.
  3. Using the join() method, we concatenate all lines into a single string.
  4. The write() method writes the concatenated string to the file.

Output (Content of output.txt):

4. Writing Multiple Lines to a File Without Newline Characters

If we forget to add newline characters, all lines will be written as a single continuous string.

main.py

</>
Copy
# Open the file in write mode
with open("output.txt", "w") as file:
    lines = ["First Line", "Second Line", "Third Line"]

    # Writing lines without newline characters
    file.writelines(lines)

print("File written successfully!")

Explanation:

1. The file is opened in write mode. 2. The list lines contains strings, but no newline characters (\n) are added. 3. The writelines() method writes them as a single line without line breaks.

Output (Content of output.txt):

To ensure proper formatting, always include newline characters (\n) at the end of each line.

Conclusion

In this tutorial, we have covered multiple ways to write multiple lines to a file:

  1. write() in a Loop: Writes lines one by one.
  2. writelines() Method: Writes a list of lines efficiently.
  3. Using join() Method: Converts a list to a single string before writing.
  4. Without Newline Characters: Demonstrates the importance of using \n.

Using writelines() is the most efficient method, but ensure that newline characters are included when required.