Python File Handling Practice Questions-3
Python File Handling Practice Questions-3
4. Write the statement to close the file “test.txt” which is associated with file
object named “fo”.
9. Write a program to read entire content from the file named “test.txt”
10. Write the output of the following, if the data stored in file “try.txt” is given
below. (“f” is the file object associated with file)
print(f.read(4))
print(f.read(5))
print(f.read())
ANSWER KEY
1. Ans. r+ mode will return error if file does not exist while w+ mode will create a new
file if file does not exist.
2. Write mode over writes the existing data while append mode add the new data at the
end of the file.
3. Ans. close()
4. Ans. fo.close()
5. NO (ANS)
readlines() : This function will read all the lines from the files.
8. Ans.
f = open(“data.txt”,”r”)
data = f.read(10)
print(data)
9. Ans.
f = open(“test.txt, “r”)
d = f.read()
print(d)
Try b
ut never cry