How to write a list to a file and read a list from file using Python

Many a times we need to store list in a file for later usage, in such cases, its better to store list in a file and read this file into list whenever we need to use. Python has very easy methods for achieving this.

 

https://gist.github.com/conquistadorjd/1ca011391e4824d30798a7535a0aad97#file-list_to_file_01-py

Output of the program

*** Program Started ***
lines : ['one', 'two', 'three', 'four', '']
type of lines : <class 'list'>
line : one
line : two
line : three
line : four
line :
*** Program Completed ***

file generated is as below

https://gist.github.com/conquistadorjd/1ca011391e4824d30798a7535a0aad97#file-list_to_file-txt

Here is another version of same program

https://gist.github.com/conquistadorjd/1ca011391e4824d30798a7535a0aad97#file-list_to_file_02-py

output and file generated is same except file opening closing styles are different.

CategoriesUncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *