How to Write Text File in Python?

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


Hi Guys,

This simple article demonstrates of how to create text file in python. you'll learn how to write text file in python. if you have question about how to make text file in python then I will give simple example with solution. if you want to see example of python create text file example then you are a right place.

In Python, there are a number different ways to make a new text file. To create a text file, we will utilise the functions open() and write(). You can use the examples I provide to start a new text file in Python. So let's go over each example one by one.

So let's see bellow example:

Example 1: Python Create Text File

We shall employ 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
New text file content line!

Example 2: Python Empty Create Text File

We shall employ 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 without text.

Example 3: Python Create Multiline Text File

We shall employ 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 4: Python Create Multiline Text File with List

We shall employ 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

It will help you....

Happy Pythonic Coding!