FH Open (Filename,'r') All - Lines FH - Readlines FH - Close
FH Open (Filename,'r') All - Lines FH - Readlines FH - Close
A common way to work with files in Python is to create file handler with “open”
statement and work with the file. After finishing the work with the file, we need
to close the file handler with close statement. For example, if we want to read all
lines of a file using Python , we use
fh = open(filename,'r')
all_lines = fh.readlines()
fh.close()
Often, it is hard to remember to close the file once we are done with the file.
Python offers an easy solution for this. We can use with statement in Python such
that we don‟t have to close the file handler. The with statement creates a context
manager and it will automatically close the file handler for you when you are
done with it. Here is an example using with statement to read all lines of a file.
with open(filename,'r') as fh
all_lines = fh.readlines()
We can also use with statement to open more than one file. Here is an example of
using with statement in Python to open one file for reading and another file for
writing.
Binary files: In this type of file, there is no terminator for a line and the data
is stored after converting it into machine understandable binary language.
tell() method:
Access modes govern the type of operations possible in the opened file. It refers
to how the file will be used once it‟s opened. These modes also define the
location of the File Handle in the file. File handle is like a cursor, which
defines from where the data has to be read or written in the file. Sometimes it
becomes important for us to know the position of the FileHanlde. tell() method
can be used to get the position of File Handle. tell() method returns current
position of file object. This method takes no parameters and returns an integer
value. Initially file pointer points to the beginning of the file(if not opened in
append mode). So, the initial value of tell() is zero.
syntax : file_object.tell()
Output :
0
# Example 2: Position of File Handle after reading data from file.
fp = open("sample.txt", "r")
fp.read(8)
print(fp.tell())
fp.close()
Output :
8
# Example 3: For binary files. Let‟s create a binary file and we will notice the
position of handle before writing and after writing to binary file.
fp = open("sample2.txt", "wb")
print(fp.tell())
fp.write(b'1010101')
print(fp.tell())
fp.close()
Output :
0
7
seek() method
In Python, seek() function is used to change the position of the File Handle to
a given specific position. File handle is like a cursor, which defines from where
the data has to be read or written in the file.
"Code is like humor. When you have to explain it, it‟s bad."
f = open("GfG.txt", "r")
f.seek(20)
print(f.tell())
print(f.readline())
f.close()
Output:
20
When you have to explain it, it‟s bad.
Example 2: Seek() function with negative offset only works when file is opened
in binary mode. Let‟s suppose the binary file contains the following text.
b'Code is like humor. When you have to explain it, its bad.'
f.close()
Output:
47
, its bad.