What is the expected output of the following code if there is no file named non_existing_file inside the working directory?
What is the expected output of the following code if there is no file named non_existing_file inside the working directory?
The code snippet attempts to open a file named 'non_existing_file' in write mode ('w'). This action will succeed because opening a file in write mode creates the file if it does not exist. Consequently, the code prints '1'. However, the following line attempts to read from the file using readline(), which is not allowed in write mode and raises an IOError. Therefore, the print statement in the except block executes, printing '3'. The final output of the code is '1 3'.
D is correct
Agree, D is correct: try: f = open("linije","w") print(1, end="") s = f.readline() print(2, end="") except IOError as error: print(3, end="") else: f.close() print(4,end="")
answer: D. 1 3
D s = f.readline() --> io.UnsupportedOperation: not readable
There is no IO error as w creates a new file. The only operation which raises IO error is either r or r+
Ioerror since variable was open with w not r
The IOError exception is raised by the line s = f.readline() because the file was opened in write mode ('w'), and readline() tries to read from a file opened in write mode, which is not allowed.
D D D D D D D
It will give IOError. So answer is D
How can the 'Suggested answer' C ever be correct? That doesn't make any sense. Answer is D
D is correct
D is correct answer
The correct and only answer is D: 1 3
The correct answer is D 1 3