Write a program to Read the Contents of a File in Python
In this tuturial we are going to learn about reading the Contents of a File in Python.
a=str(input("Enter the name of the file with .txt extension:")) file2=open(a,'r') line=file2.readline() while(line!=""): print(line) line=file2.readline() file2.close()
- User must enter a file name.
- The file is opened using the open() function in the read mode. 3.The readline() outside the while loop is used to read the first line of the file.
- Inside the loop, the first line is first printed, and then the remaining lines are read and subsequently printed.
- This continues this the end of file.
- The file is then closed.