Computer Science
How are the following codes different from one another?
1.
fp = open("file.txt", 'r')
fp.read()
2.
fp = open("file.txt", 'r')
Answer
The first code opens 'file.txt' in read mode, initializes a file object (fp) for further file operations, and reads its entire content using fp.read()
. The second code also opens 'file.txt' in read mode, initializes a file object (fp) for further file operations, but it doesn't perform any read operation.
Related Questions
Write appropriate statements to do the following:
(a) To open a file named "RESULT.DAT" for output.
(b) To go to the end of the file at any time.
Write a program to add two more employees' details (empno, ename, salary, designation) to the file "emp.txt" already stored in disk.
What is the output of the following code fragment? Explain.
fout = open("output.txt", 'w') fout.write("Hello, world! \n") fout.write("How are you?") fout.close() f = open("output.txt") print(f.read())
Write the output of the following code with justification if the contents of the file "ABC.txt" are:
"Welcome to Python Programming!"
f1 = open("ABC.txt", "r") size = len(f1.read()) print(size) data = f1.read(5) print(data)