2
QuizMultiple Choice

Searching Complex Data

Question 2 • Introduction to Computer Science B – 4-232

You are going to open a file in your program. The program is saved in a folder, which has a subfolder called "textFiles."In the textFiles folder is a text file called "books.txt."Which line of code will open books.txt?

Answer
A
fileIn = open("books.txt","r")
B
fileIn.open("textFiles/books.txt","r")
C
fileIn = open("textFiles/books.txt","r")
D
fileIn.open("books.txt","r")

Explanation

The file is inside the program’s textFiles subfolder, so its relative path is "textFiles/books.txt". The open() function takes that path and the mode "r" to open the file for reading, and its result is assigned to fileIn. Therefore, the correct line is `fileIn = open("textFiles/books.txt","r")`.

AI-written and checked against the verified answer.