How to Generate Text File from List in Python?

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

Hi Guys,

Are you looking for an example of how to create a text file from a list in python. you can understand the concept of python generating text file from list. This post will give you a simple example of python writing a text file from list. This tutorial will give you simple example of python create text file from list. Follow bellow tutorial step of python adds text file to list.

We'll make a list called "myLines" in this example and add some content to it. Next, we'll add lines to the text file one at a time using a for a loop. Let's look at the straightforward example below:

So let's see bellow example:

Example 1
myLines = ["Hi!", "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:
print("New text file created successfully!")

It will create readme.txt file with following text.

This is body
Thank you

It will help you....

Happy Pythonic Coding!