Professional Documents
Culture Documents
File Handling Part 1
File Handling Part 1
File Handling
Need of File
tell() Function:
tell method can be used to get the position of file
handle.This method takes no parameters and
returns an integer value .Initially file pointer points
to the beginning of the file.
Position of data 1 2 3 4 5 6 7 8 9 10
Data of file A B C D E F G H I
File handle or
pointer
Reading from a File-
File Pointer/File handle and read() function
f=open(“x.txt”,”r”)-
File pointer will lie at the starting at of the file means zero
position
Position of data 1 2 3 4 5 6 7 8 9 10
Data of file A B C D E F G H I
File handle or
pointer
Reading from a File-
File Pointer/File handle and read() function
f.read(1)File pointer will lie at the starting at of the file
meansof zero
Position data position
1 2 3 4 5 6 7 8 9
Data of file A B C D E F G H I
File handle or
pointer
f.read(3)
Position of data 1 2 3 4 5 6 7 8 9
Data of file A B C D E F G H I
File handle or
pointer
Reading from a File-
File Pointer/File handle and read() function
seek() Function:
Seek() function is used to change the position of
file handle to given specific position.It is a void
function .It will not return any value.
Syntax:
f.seek(offset , from_what ), where f is a file pointer
or file handle
Parameters:
offset-Number of position to move forward
from_what-It defines the point of reference
return-Does not return any value
seek() Function
What a "line" means to your program.
It is entirely possible to make it recognize line
terminators of different types, and even of mixed
types.
In that case, it may be useful to know that:
Lines of Windows text files are terminated by the
two-byte sequence "\r\n",
whereas lines of Unix (including OS X) text files
are terminated by a single newline.
Lines of classic MacOS text files were terminated
by single carriage returns.
Some other systems have other conventions or
support multiple conventions.
seek() Function
seek() Function:
The reference point is selected by the from_what
argument.It accepts three values:
0-sets the reference point at the beginning of the file.We
cannot use negative value
1:sets the reference point at the current file position
2:sets the reference point at the end of the file
This function will convert a string into list. Basically depends upon the
space or any other sign.
# 1Program in python to read each word separately
(Example of Split())
#2Program in python to read entire text file
#3Program in python to append text at the end of
text file
#4Program in python to read each word separately
(Count no. Of words)
#5Program in python to read each word separately
(To show word with maximum length)