How to import Text file in Python

In Python, you can import modules, but not text files. If you want to obtain data from a text file, you can use the Python file operations. Python provides inbuilt functions for creating, writing, and reading files.
Assume we have the following text file:

To open the file, we can use the built-in file function open()
. The open()
function returns a file object, which has a read()
method for reading the content of the file:
f = open("Students.txt") print(f.read())
Output:
Name Age Grade ABEL 20 A BINDU 21 A CHRISTY 20 A YOUSUF 21 A KRISHNA 20 A
Subscribe
Login
Please login to comment
0 Discussion