How to read a number of characters from a text file using Python?


Python has a built-in file creation, writing, and reading capabilities. In Python, there are two sorts of files that can be handled: text files and binary files (written in binary language, 0s, and 1s).

Let us understand how to open a file in python. Python is a good general purpose programming language that has a lot of helpful file IO functions and modules in its standard library. You can use the built in open() function to open a file object for either reading or writing. You can use it to open a file in the following way.

Syntax

The syntax of the open() method is as follows.

File = open(“txt_file_name” ,”access_mode”)

Example

The following is an example to open a file named example.txt in read mode.

file= open("example.txt", "r")

Output

On executing the above program, the following output is generated.

The file example.txt is opened in read mode.

Reading a file in Python

To read the contents of a file, use file_name.read(size), which reads a specified amount of data and returns it as a string. The size is a number argument that can be used. The complete contents of the file will be read and returned if size is omitted or negative. Otherwise, the maximum number of bytes read and returned is size. The file_name.read() will return an empty string if the file has reached its end ("").

Example 1

In the following example, if you wish to read 10 ASCII characters, the value 15 has to be passed as a parameter. Then, close the file using the close() function.

file = open('example.txt', 'r')
print(file.read(15))
file.close()

Output

On executing the above program, the following output is generated.

Reading is impo

Using seek() and read()

To read a specific number of characters from a particular position in the file we can use seek() function. The seek() function in Python is used to shift the file handler's position to a certain location. A file handle functions similarly to a cursor, indicating where data should be read or written in the file.

The read() function reads a specified number of bytes from a file and returns them. The default value is 1, which means the entire file.

Example 2

The following is an example to read characters from a file. Firstly, the file is opened in the read only mode. To start reading from a specific position in a file, seek() function is used. The read characters are printed using the print() function. The file is then closed using the close() function.

#python program to read characters from a file
#Opening a file in read access mode
file = open('example.txt', 'r')
#To start reading from a specific position in a file, for say 10
#the read happens from the 10th position
file.seek(10)
#printing 15 characters using read()
print(file.read(55))
#closing the opened file   
file.close()

Output

On executing the above program, the following output is generated.

important because it develops our thoughts, gives us e

Updated on: 11-May-2023

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements