Computer Science
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")
Answer
fobj = open("c:\\test.txt", "a")
Reason — The syntax for 'open()' is: <file variable>/<file object or handle> = open(file_name, access_mode)
. Therefore, according to this syntax, fobj = open("c:\\test.txt", "a")
is the correct statement to open a file for writing. The access mode "a" indicates that the file should be opened in append mode.
Related Questions
To open a file c:\test.txt for reading, we should give the statement:
- file1= open("c:\ test.txt", "r")
- file1 = open("c:\\ test.txt", "r")
- file1 = open(file = "c:\ test.txt", "r")
- file1 = open(file = "c:\\s test.txt", "r")
To open a file c:\test.txt for writing, we should use the statement:
- fobj = open("c:\test.txt", "w")
- fobj = open("c:\\test.txt", "w")
- 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 two characters from a file object fobj, the command should be:
- fobj.read(2)
- fobj.read()
- fobj.readline()
- fobj.readlines()