read a file into list and write list into file in python code example
Example 1: convert python list to text file
# define list of places
places = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
with open('listfile.txt', 'w') as filehandle:
for listitem in places:
filehandle.write('%s\n' % listitem)
Example 2: python read file in string list
# read file in a string list
with open(fileName) as f:
lineList = f.readlines()
Example 3: python read file into a list
f = open(filename, "r")
listItems = f.read().splitlines()