144k views
5 votes
Write a statement that takes a variable named file_object that contains a file object and reads its contents into a Python list of lines. Store the resulting list into a variable named file_contents.Write a statement that takes a variable named file_object that contains a file object and reads its contents into a Python list of lines. Store the resulting list into a variable named file_contents.

1 Answer

3 votes

Answer:

Answered below

Step-by-step explanation:

#file is first opened and saved into file_object variable.

file_object = open('new_file.txt', 'r')

file_contents = file_object.readlines()

#the readlines() reads the contents of the file, line by line and returns a list of lines. Therefore, file_contents contains a list of lines read from file object and can be iterated over to get each line.

User Camaleo
by
3.6k points