Python program to read the contents of a file

Editorial Staff - - Python Programming


We are going to share a Python program to read the contents of a file. If you are a python beginner and want to start learning the python programming, then keep your close attention in this tutorial as I am going to share a Python program to read the contents of a file.

Python program to read the contents of a file code

python program and execute it with the help of python compiler. At the end of this tutorial, We have shared the output of below program.

Python Program to Read the Contents of a File code
1
2
3
4
5
6
7
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()

Program Output:

Output:
Enter the name of the file with .txt extension: test-file.txt
This is an example of Python programs.

To increase your Python knowledge, practice all Python programs, here is a collection of 100+ Python problems with solutions.


About: Editorial Staff

Editorial Staff at FreeWebMentor is a team of professional developers leads by Prem Tiwari


Programs With Output