Python Programming – Reading File

In this Page, We are Providing Python Programming – Reading File. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Python Programming – Reading File

Reading file

To read a file’s contents, call read (size) method, which read size bytes of data and returns it as a string, size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned; otherwise, at most size bytes are read and returned. If the end of the file has been reached, read () will return an empty string (‘ ‘).

>>> f.read ( 19 )
' This is first line .'

There is a tell () method, which returns an integer giving the file object’s current position in the file, measured in bytes from the beginning of the file.

>>> f.tell ( )
19L

To change the file object’s position, use seek (off set, from_what) method. The position is computed from adding offset to a reference point; the reference point is selected by the f rom_what argument. A f rom_what value of 0 measures from the beginning of the file, 1 uses the current file position, and 2 uses the end of the file as the reference point, f rom_what can be omitted and defaults to 0, using the beginning of the file as the reference point.

>>> f.seek ( -52 , 2 )
>>> f . read ( )
' This is third line . \ n \ nThis is fourth and last line . '
>>> f . tell ( )
99L
>>> f . seek ( 0 , 0 )
>>> f . tell ( ) 
0L
>>> f . read ( )
'This is first line.\n\nThis is second line.\n\nThis is third line.\n\nThis is fourth and last line.'

When relevant operations on a file are finished, use close () method to close the file and free-up any system resources taken up by the open file. After calling close (), attempt to use the file object will automatically fail.

>>> f . close ( ) 
>>> f . read ( )
Traceback ( most recent call last ) :
File "<stdin>", line 1, in ?
ValueError: I/O operation on closed file

There is also a readlirie ( ) method that read a single line from the file; a newline character (\n) is left at the end of the string, and is only omitted on the last line of the file, if the file does not end in a newline. If readline ( ) returns an empty string, the end of the file has been reached, while a blank line is represented by ‘ \n ‘.

>>> f=open ( ' C : / test / list . txt ' , ' r ' )
>>> f . readline ( )
' This is first line . \ n '
>>> f . readline ( )
' \ n '
>>> f . readline ( )
' This is second line . \ n '
>>> f . readline ( )
' \ n '
>>> f . readline ( )
' This is third line . \ n '
>>> f.readline ( )
' \ n '
>>> f,readline ( )
' This is fourth and last line . '
>>> f . readline ( )
' ' 
>>> f . close ( )

For reading lines from a file, one can also loop over the file object. This is memory efficient, fast, and leads to simple code:

>>> f=open ( ' C : / test / list . txt ' , ' r ' )
>>> for line in f :
. . . print line ,
This is first line.

This . is second line .

This is third line .

This is fourth and last line.
>>> f . close ( )

If there is a requirement to read all lines of a file in a list, one can do list (f) or f .readlines ( )

>>> f=open ( ' C : / test / list . txt ' , ' r ' )
>>> list ( f ) 
[ ' This is first line . \ n ' , ' \ n ' , ' This is second line . \ n ' , ' \ n ' , ' This is third line . ' \ n ' , ' \ n ' , ' This is fourth and last line . ' ]
>>> f.close ( )
>>>
>>> f=open ( ' C : / test / list . txt ' , ' r ' )
>>> f . readlines ( )
[ ' This is first line . ' \ n ' , ' \ n ' , ' This is second line . ' \ n ' , ' \ n ' , ' This is third-line. ' \ n ' ' \ n ' , ' This is fourth and last line . ' ]
>>> f . close ( )