write a python program to read specified number of lines from the specified file using functions

Given a text file as an input and the number of lines to be printed from the text file.  The Python program should  read a specified number of lines from the file and print it onto the std out.

You should write a Python function to perform this task.

The Python function  should take two input arguments, that is file name and number,  Python function should open the file in read mode,  using the value to iterate all the  required number of lines  and  at the same time print it.

Close the file at the end.

 

write a python program to read specified number of lines from the specified file using functions

 



Write a Python Program to Read Specified Number of Lines From The Specified File Using Functions

Define new function read_file(), Given two input arguments that are filename and number. You just try Block 2 to open the file in a read mode.  if the file is not found, catch and exception  using expect block, print an error message and exit the program.

def read_file(filename, number):
   try:
       fd_read = open(filename, "r")
   except FileNotFoundError:
       print("File not found !")
       exit(-1)




If the file is present and able to open in the read mode, set the read file descriptor.  use the else block to iterate over a number of lines. Until n number is reduced to 0,  keep iterating,  read the line, and print the line onto stdout using print() function call.

   else:
       while number != 0:
           line = fd_read.readlines(2)
           print(line, end="")
           number = number - 1
       fd_read.close()

 



Once the while loop completes, close the file descriptor using close() function call.

In the main program, take a variable file name and use the input function to ask the user to enter the file name.  take another integer variable and ask the user to enter the number of lines to be printed.

Once we get the file name and number of lines to be printed,  print a user-friendly message using print function  and call the function read_file() by providing filename and line number as an input argument.


filename = input("Enter Filename : ")
line_numbers = int(input("Enter Number of Lines : "))
print("Contents of File is : …")
read_file(filename, line_numbers)




Complete Python Program
=============================
def read_file(filename, number):
   try:
       fd_read = open(filename, "r")
   except FileNotFoundError:
       print("File not found !")
       exit(-1)
   else:
       while number != 0:
           line = fd_read.readlines(2)
           print(line, end="")
           number = number - 1
       fd_read.close()


filename = input("Enter Filename : ")
line_numbers = int(input("Enter Number of Lines : "))
print("Contents of File is : …")
read_file(filename, line_numbers)

 



INPUT : Contents of sample.txt
====================================

Hello
How
Are
You
???




OUTPUT
=====================================

Enter Filename : sample.txt
Enter Number of Lines : 2
Contents of File is …

Hello
How 




Conclusion: Try running the above program in your environment and comment down below if you have any suggestions or queries.


Can you try solving this problem using a for loop ??

 


Post a Comment

0 Comments