How do you open a file named data.txt in read mode in Python?
A. file = open('data.txt', 'r')
B. file = open('data.txt', 'read')
C. file = open('data.txt', 'read mode')
D. file = open('data.txt')
Select an option to see the answer and solution.
What is the purpose of the with statement in file handling?
A. It ensures that the file is properly closed after usage
B. It creates a new file
C. It reads the file contents
D. It deletes the file
Select an option to see the answer and solution.
How can you write data to a file in Python?
A. By using the write() method on a file object
B. By using the save() function
C. By using the put() function
D. By using the append() method
Select an option to see the answer and solution.
What does the read() method of a file object do?
A. It reads the entire contents of the file
B. It writes data to the file
C. It closes the file
D. It deletes the file
Select an option to see the answer and solution.
How can you read a single line from a file in Python?
A. By using the readline() method on a file object
B. By using the line() function
C. By using the get_line() method
D. By using the read() method
Select an option to see the answer and solution.
What is the purpose of the seek() method in file handling?
A. It changes the file position to a specified offset
B. It searches for a string in the file
C. It appends data to the end of the file
D. It reads a specific line
Select an option to see the answer and solution.
How can you read the entire contents of a file line by line in Python?
A. By using a for loop with the file object
B. By using the readlines() method on a file object
C. By using the readline() method on a file object
D. By using the line() function
Select an option to see the answer and solution.
What is the output of the following code:with open('data.txt', 'a') as file: file.write('Appended line.') with open('data.txt', 'r') as file: content = file.read() print(content)
A. Contents of data.txt with the appended line
B. An error will occur
C. Appended line.
D. file object
Select an option to see the answer and solution.
How can you create a new file in Python if it doesn't exist?
A. By using the 'x' mode when opening the file
B. By using the 'create' keyword
C. By using the new() function
D. By using the 'c' mode when opening the file
Select an option to see the answer and solution.
To open a file c:\scores.txt for reading, we use . . . . . . . .
A. infile = open(“c:\scores.txt”, “r”)
B. infile = open(“c:\\scores.txt”, “r”)
C. infile = open(file = “c:\scores.txt”, “r”)
D. infile = open(file = “c:\\scores.txt”, “r”)
Select an option to see the answer and solution.
To open a file c:\scores.txt for appending data, we use . . . . . . . .
A. outfile = open(“c:\\scores.txt”, “a”)
B. outfile = open(“c:\\scores.txt”, “rw”)
C. outfile = open(file = “c:\scores.txt”, “w”)
D. outfile = open(file = “c:\\scores.txt”, “w”)
Select an option to see the answer and solution.
How can you close a file in Python?
A. By using the close() method on a file object
B. By using the end() function
C. By using the exit() method
D. By using the terminate() method
Select an option to see the answer and solution.
What is the purpose of the tell() method in file handling?
A. It returns the current file position
B. It reads the file contents
C. It closes the file
D. It appends data to the file
Select an option to see the answer and solution.
To open a file c:\scores.txt for writing, we use . . . . . . . .
A. outfile = open(“c:\scores.txt”, “w”)
B. outfile = open(“c:\\scores.txt”, “w”)
C. outfile = open(file = “c:\scores.txt”, “w”)
D. outfile = open(file = “c:\\scores.txt”, “w”)
Select an option to see the answer and solution.
Which of the following statements are true?
A. When you open a file for reading, if the file does not exist, an error occurs
B. When you open a file for writing, if the file does not exist, a new file is created
C. When you open a file for writing, if the file exists, the existing file is overwritten with the new file
D. All of the mentioned
Select an option to see the answer and solution.
What is the purpose of the write() method in file handling?
A. It writes data to the file
B. It reads the file contents
C. It creates a new file
D. It closes the file
Select an option to see the answer and solution.
To read two characters from a file object infile, we use . . . . . . . .
A. infile.read(2)
B. infile.read()
C. infile.readline()
D. infile.readlines()
Select an option to see the answer and solution.
What is the output of the following code:file = open('data.txt', 'r') content = file.read() print(content)
A. Contents of data.txt
B. An error will occur
C. file object
D. r
Select an option to see the answer and solution.
What is the output of the following code:with open('data.txt', 'w') as file: file.write('Hello, world!')
A. The file data.txt will contain Hello, world!
B. An error will occur
C. The file data.txt will be empty
D. Hello, world!
Select an option to see the answer and solution.
What will be the output of the following code:with open('data.txt', 'r') as file: lines = file.readlines() print(len(lines))
A. The number of lines in data.txt
B. An error will occur
C. The content of data.txt
D. The file object file
Select an option to see the answer and solution.