Reading a file using “with”

Opening a file using with keyword

1
2
3
4
5
6
7
8
#file open example using "with" (recomemded)
with open('raw_corpus.txt') as fp:
    lines = fp.read().split("n")   #here lines contains entire file contents


#to access file contents line by line
for line in lines:
    print (line)

When you run this python script, the contents of the file ‘raw_corpus.txt’ are printed line by line.

Leave a Reply

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