how to write a list to a text file python code example
Example 1: how to put a text file into a list python
# name.txt
david
mary
john
with open('names.txt', 'r') as f:
myNames = [line.strip() for line in f]
# Result
['david','mary','john']
Example 2: list to text file python
with open('your_file.txt', 'w') as f:
for item in my_list:
f.write("%s\n" % item)