M.Sc Python Programming Assignment 24

M.Sc Python Programming Assignments

Assignment 24

Question:

Write a Python program to read a file line by line store it into an array.

Code:

				
					def read_file_lines(file_name):
    lines = []

    with open(file_name, 'r') as file:
        for line in file:
            lines.append(line.strip())

    return lines

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

lines = read_file_lines(file_name)

print("File contents stored in array:")
for line in lines:
    print(line)

				
			

Output:

				
					Enter the file name: goku
File contents stored in array:
lucifer

				
			
Tech Amplifier Final Logo