M.Sc Python Programming Assignment 28

M.Sc Python Programming Assignments

Assignment 28

Question:

Write a Python program to read a random line from a file

Code:

				
					import random

def read_random_line(file_name):
    with open(file_name, 'r') as file:
        lines = file.readlines()
        random_line = random.choice(lines)
        return random_line.strip()

# Example usage
file_name = input("Enter the file name: ")

random_line = read_random_line(file_name)

print("Random line from the file:")
print(random_line)

				
			

Output:

				
					Enter the file name: goku
Random line from the file:
run

				
			
Tech Amplifier Final Logo