Computer Science
To read two characters from a file object fobj, the command should be:
- fobj.read(2)
- fobj.read()
- fobj.readline()
- fobj.readlines()
Python Data Handling
2 Likes
Answer
fobj.read(2)
Reason — The command fobj.read(2)
is used to read two characters from a file object fobj. The read() method without arguments (fobj.read()
) reads the entire content of the file, while fobj.readline()
reads a single line, and fobj.readlines()
reads all lines and returns them as a list.
Answered By
2 Likes
Related Questions
To open a file c:\test.txt for appending data, we can give the statement:
- fobj = open("c:\\test.txt", "a")
- fobj = open("c:\\test.txt", "rw")
- fobj = open(file = "c:\test.txt", "w")
- fobj = open(file = "c:\\test.txt", "w")
Which of the following statements is/are true?
- When we open a file for reading, if the file does not exist, an error occurs.
- When we open a file for writing, if the file does not exist, a new file is created.
- When we open a file for writing, if the file exists, the existing file is overwritten with the new file.
- All of these.
To read the entire contents of the file as a string from a file object fobj, the command should be:
- fobj.read(2)
- fobj.read()
- fobj.readline()
- fobj.readlines()
What will be the output of the following snippet?
f = None for i in range(5): with open("data.txt", "w") as f: if i > 2: break print(f.closed)
- True
- False
- None
- Error