Python Create Multiline Text File Example

Published On: 16/12/2022 | Category: Python


Hi Guys,

This tutorial is focused on how to make multi line text file in python. let’s discuss about python create multiline text file example. you can see python create a new multiline text file. I’m going to show you about how to create multiline text file in python.

A new multiline text file can be created in Python in a few different ways. To create a text file, we will utilise the functions open() and write(). I'll give you a few examples of how to create new text files in Python with many lines. So let's go over each example one by one.

So let's see bellow example:

Example 1: Python Create Multiline Text File

We shall utilize the open() function in this example. It requires two parameters, the first for the filename and the second with the letter "w" for opening a file for writing. then, to write several text lines to a file, we will utilise the writelines() function. notice the output from the example below:

main.py
# create a new text file with multi lines code
with open('readme.txt', 'w') as f:
    line1 = "Hi Tuts-Station.com! \n"
    line2 = "This is body \n"
    line3 = "Thank you"
  
    f.writelines([line1, line2, line3])
  
print("New text file created successfully!")
Output

It will create readme.txt file with following text.

Hi Tuts-Station.com!
This is body
Thank you

Example 2: Python Create Multiline Text File with List

We must use the open() function in this example. It requires two parameters, the first for the filename and the second with the letter "w" for opening a file for writing. Then, using a Python list on a file, we will write many text lines using the writelines() function. notice the output sample below:

main.py
myLines = ["Hi Tuts-Station.com!", "This is body", "Thank you"]
  
# create a new text file with multi lines code
with open('readme.txt', 'w') as f:
    for line in myLines:
        f.write(line)
        f.write('\n')
  
print("New text file created successfully!")
Output

It will create readme.txt file with following text.

Hi Tuts-Station.com!
This is body
Thank you

Example 3: Python Create Text File

We should use the open() function in this example. It requires two parameters, the first for the filename and the second with the letter "w" for opening a file for writing. then we'll write text to a file using the write() function. notice the output from the example below:

main.py
# create a new text file code
with open('readme.txt', 'w') as f:
    f.write('New text file content line!')
  
print("New text file created successfully!")
Output

It will create readme.txt file with following text.

New text file content line!

Example 4: Python Empty Create Text File

We should use the open() function in this example. It requires two parameters, the first for the filename and the second with the letter "w" for opening a file for writing. then we just use the close() function to close the file. notice the output from the example below:

main.py
# create new empty text file code
open('readme.txt', 'w').close()
  
print("New empty text file created successfully!")
Output

It will create readme.txt file with following text.

It will help you....

Happy Pythonic Coding!