Python Generate Text File from List Example

By Hardik Savani October 30, 2023 Category : Python

Hi Friends,

In this tutorial, we will go over the demonstration of python create text file from list. step by step explain how to create text file from list in python. This article goes in detail on python generate text file from list. If you have a question about python write text file from list then I will give a simple example with a solution. you will do the following things for python add text file to list.

In this example, we will create "myLines" list with some content. Then we will use for loop and add one by one line to the text file. so let's see a below simple example:

Example 1:

main.py

myLines = ["Hi ItSolutionstuff.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 ItSolutionstuff.com!
This is body
Thank you

I hope it can help you...

Tags :
Shares