0% found this document useful (0 votes)
54 views4 pages

FH Open (Filename,'r') All - Lines FH - Readlines FH - Close

Uploaded by

Priyanshu Tyagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views4 pages

FH Open (Filename,'r') All - Lines FH - Readlines FH - Close

Uploaded by

Priyanshu Tyagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

05 July 2021(Monday)

“with” statement in Python to Open a file

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.

with open(in_filename) as in_file, open(out_filename, 'w') as out_file:

Python tell() function


Python too supports file handling and provides inbuilt functions for creating,
writing and reading files. There are two types of files that can be handled in
python, normal text files and binary files (written in binary language, 0s and 1s).
 Text files: In this type of file, Each line of text is terminated with a special
character called EOL (End of Line), which is the new line character („\n‟) in
python by default.

 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()

# Example 1: Position of File Handle before reading or writing to file.


fp = open("myfile.txt", "r")
print(fp.tell())
fp.close()

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.

Syntax: f.seek(offset, from_what), where f is file pointer


Parameters:
Offset: Number of positions to move forward
from_what: It defines point of reference.
Returns: Does not return any value

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

1: sets the reference point at the current file position

2: sets the reference point at the end of the file

By default from_what argument is set to 0.


Note: Reference point at current position / end of file cannot be set in text
mode except when offset is equal to 0.
Example 1: Let’s suppose we have to read a file named “GfG.txt” which contains
the following text:

"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.'

# Python code to demonstrate


# use of seek() function

# Opening "GfG.txt" text file


# in binary mode
f = open("data.txt", "rb")

# sets Reference point to tenth


# position to the left from end
f.seek(-10, 2)

# prints current position


print(f.tell())

# Converting binary to string and


# printing
print(f.readline().decode('utf-8'))

f.close()

Output:
47
, its bad.

You might also like