Write a Python Program to Read The Contents of a File

Given a file as an input to the program you have to write a Python program to  open the file in read mode, read contents of the file  and bring the data that is read from the file onto the console.

The file should be closed after the task is done.

 

Write a Python Program to Read The Contents of a File

 


write a python program to read the contents of a file

Using the filename variable, ask the user to enter the file name using input function call,  and store the string in the filename  variable.

filename = input("Enter a Filename to Read : ")

 



Use a try block to  open the file in the read mode and save the file descriptor. If the file is open successfully read the contents of the file and store it into a variable named data.

Use the print function to print the variable data.

try:
   fd_read = open(filename, "r")
   data = fd_read.read()
   print(data)

 



Using an exception catch the file not found exception, If the file is not found then print file not found error message  and exit the program.

except FileNotFoundError:
   print("File not Found !")
   exit(-1)



Complete program
======================

filename = input("Enter a Filename to Read : ")

try:
   fd_read = open(filename, "r")
   data = fd_read.read()
   print(data)
except FileNotFoundError:
   print("File not Found !")
   exit(-1)

 

 

There is also a practice to  use the else block with the try,  if the try is successful then the else block will be executed.  so let's open the file using a try block and in the else  code block  let's read the contents of the file using the file descriptor and close the file

Complete program using try, except and else block
========================================

filename = input("Enter a Filename to Read : ")
fd_read = None
 
try:
   fd_read = open(filename, "r")
except FileNotFoundError:
   print("File not Found !")
   exit(-1)
else:
   data = fd_read.read()
   print(data)
 

 


With statement as a benefit over normal open(),  using the with keyword actually closes the file automatically once the code block is completed. The above program can also be written using a with statement as shown below.

 

write a python program to read the contents of a file using with statement

Take a variable file name to store the input string filename and ask a user to enter a filename using input function call.  then using the try block and with the keyword open the file in the read mode.  use the with cloth code block to read the contents of the file and print the data.

filename = input("Enter a Filename to Read : ")
try:
   with open(filename, "r") as fd_read:
       data = fd_read.read()
       print(data)

 



Use the exception to check if the file is not present.  if the file not found is caught by an exception then print and appropriate message and exit the program

except FileNotFoundError:
   print("File not Found !")
   exit(-1)

 



Final program
======================

filename = input("Enter a Filename to Read : ")
try:
   with open(filename, "r") as fd_read:
       data = fd_read.read()
       print(data)
except FileNotFoundError:
   print("File not Found !")
   exit(-1) 

 




Conclusion
================

Try running the program  by creating a sample file in the current working directory and pass the file name as input to the  program. 


Comment down below if you have any suggestions or queries.


Post a Comment

0 Comments