Q. What is the output of following code fragment? Explain.
out = open("output.txt", "w")
out.write("Hello, world! \n")
out.write("How are you?")
out.close()
print (open("output.txt").read())
Answer :-
The output will be:
Hello, world!
How are you?'
The first line of the code is opening the file in write mode; the next two lines write text to the file. The last line opens the file and from that reference reads the file-content. Function file() does the same as that of open(). Thus file("output.txt") will give the reference to open file, on which read() is applied.
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )