Python Data Persistence – Reading a File
Let us now read ‘top-quotes.txt programmatically by opening it with r mode. When in R mode, the file object can call read (), readline ( ) , and readlines () methods.
Out of these, the read ( ) method can read a specified number of bytes from the file, the size defaults to file size. However, if the file is very big, the available memory restrictions may not allow the entire file to be read, so you may have to provide size parameters to read bytes at once.
Example
>>> file=open ( ' top-quotes . txt' , ' r') >>> text=file . read () >>> text "'The best way to predict the future is to invent it.' - Alan Kay'\nThere are only two kinds of programming languages: those people always bitch about and those nobody uses.' - Bjarne Stroustrup\ n'The only way to learn a new programming language is by writing programs in it.' -Dennis Ritchie\n'A computer would deserve to be called intelligent if it could deceive a human into believing that it was human.' - Alan Turing\nprogramming languages have a devious influence. They shape our thinking habits - Edsger W. Dijkstra\nprogrammers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program - Linus Torvalds\nA computer would deserve to be called intelligent if it could deceive a human into believing that it was human - Alan Turing" >>>file . close ( )
To read specified number of bytes from the beginning of file
Example
>>> file=open (' top-quotes . txt' , ' r' ) >>> text =file . read (65) >>> text " ' The best way to predict the future is to invent it.' - Alan Kay\n" >>> file . close ()
Reading a File Line-by-Line
The readline () method reads all bytes till a newline character ‘\n’ is encountered. It returns an empty string when no more lines are left to be read. Hence, we can call readline ( ) method in a loop till empty string is returned, (figure 5.2)
>>> file=open ( ' top-quotes . txt1 , ' r ' ) >>> while True: line=file . readline () if line=='':break print (line, end='') 'The best way to predict the future is to invent it.1 - Alan Kay 1 There are only two kinds of programming languages: those people always bitch about and those nobody uses.' - Bjarne Stroustrup 'The only way to learn a new programming language is by writing programs in it.1 -Dennis Ritchie 'A computer would deserve to be called intelligent if it could deceive a human into believing that it was human.' - Alan Turing programming languages haile a devious influence. They shape our thinking habits - Edsger W. Dijkstra programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program - Linus Torvalds A computer would deserve to be called intelligent if it could deceive a human into believing that it was human - Alan Turing The file object is a data stream that acts as an iterator. An iterator serves subsequent object every time next() function is called till stream is exhausted and Stoplteration exception is encountered. We can use the next () method on the file object to read a file line by line.
Example
f=open("top-quotes.txt", "r") while True: try: line=next(f) print (line, end=" ") except StopIteration: break f . close ( )
Or simply use a for loop over each line in the file iterator:
Example
file=open ("top-quotes . txt", " r") for line in file: print (line, end="") file. close ( )
The readlines ( ) method returns list of lines in the file.
>>> file=open( top-quotes.txt ' 'r') >>> lines=file.readlines( )