Which function is used to open a file for reading in Python?
Analysis & Theory
To open a file for reading, use open('file.txt', 'r') or simply open('file.txt').
What does the read() method return?
C
The entire file content as a string
D
The last line of the file
Analysis & Theory
The read() method returns the full content of the file as a single string.
Which method reads the file line by line into a list?
Analysis & Theory
The readlines() method returns a list where each item is a line in the file.
What does the readline() method do?
A
Reads the whole file at once
B
Reads the first 10 characters
C
Reads a single line from the file
D
Writes a line to the file
Analysis & Theory
The readline() method reads and returns the next single line from the file.
What will happen if you call read() twice in a row?
A
It reads the same content twice
B
It restarts reading from the beginning
C
The second call returns an empty string
Analysis & Theory
After the file is read completely, the file pointer is at the end, so read() returns an empty string the second time.
How do you read only the first 5 characters of a file?
Analysis & Theory
read(5) reads the first 5 characters from the file.
Which mode is used to open a file for both reading and writing?
Analysis & Theory
The 'r+' mode opens the file for both reading and writing.
What does the file object's tell() method return?
A
The total number of characters
C
The current file pointer position
Analysis & Theory
The tell() method returns the current position of the file pointer.
What does the seek(0) method do?
B
Moves the file pointer to the beginning
Analysis & Theory
seek(0) moves the file pointer to the beginning of the file so you can read it again.
Which statement ensures a file is automatically closed after reading?
Analysis & Theory
Using `with open(...) as f:` ensures the file is automatically closed after its block is executed.