Javatpoint Logo
Javatpoint Logo

How to read a text file in Python

Python provides the facility to read, write, and create files. The file can be two types - normal text and binary.

  • Text Files - This type of file consists of the normal characters, terminated by the special character This special character is called EOL (End of Line). In Python, the new line ('\n') is used by default.
  • Binary Files - In this file format, the data is stored in the binary format (1 or 0). The binary file doesn't have any terminator for a newline.

Here, we will learn to read the text file in Python.

Python takes the three required steps to read or write a text file.

  • Open a file
  • Read or Write file
  • Close file

Opening a Text File

To open the text file in Python, we can use open() function. A file object is returned when open() function takes it as an argument.

Code

Output:

This
 is line 1

This is line 2
This is line 3
This is line 4

The first argument is the file name, and the second argument is a mode of the file.

Here r means read mode.

Explanation:

The open() takes mainly two arguments the filename and mode. It returns the file object, which is also called a handle. It can be used to perform various operations to the file. We can specify the mode of the file while opening a file. The mode of file can be read r, write w, and append a. We will open the text file using the open() function.

Reading a Text File

Python provides the various function to read the file, but we will use the most common read() function. It takes an argument called size, which is nothing but a given number of characters to be read from the file. If the size is not specified, then it will read the entire file.

Code

Explanation:

Here, all the information or contents in the file are stored in the read() function. Then we will print the contents.

Closing a Text File

To close the file, we can simply use close() function to terminate the file.

Code

Example Program:

Code

Output:

This
 is line 1

This is line 2
This is line 3
This is line 4

Explanation:

In the above code, we can see the read() function read the character according to its given size from the file. The con1 variable read the next 10 characters from the last read() function. In the last line, we closed the file after performing the read operation using the close() function.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA