DFH WORKSHEET1
DFH WORKSHEET1
1. Which of the following is used for reading data from a file in Python?
a) open() b) read() c) write() d) close()
2. The mode used to open a file for writing (and creating the file if it does not
exist) is:
a) 'r' b) 'w’ c) 'a’ d) 'rb'
3. Which method is used to read a single line from a file in Python?
a) read() b) readlines() c) readline() d) next()
4. What will be the output of the following code?
f = open('file.txt', 'w')
f.write('Hello, World!')
f.close()
a) It will print "Hello, World!" to the console.
b) It will create a file named file.txt and write "Hello, World!" to it.
c) It will create a file named file.txt but not write anything to it.
d) It will give an error because write() is not allowed in this mode.
5. In Python, which function is used to open a file?
a) open() b) file( ) c) create() d) write()
12. In which mode does the file pointer start at the end of the file?
a) 'r' b) 'w' c) 'a' d) 'r+'
13. What happens if you try to read a file in 'w' mode?
a) File will be opened for reading.
b) File will be opened for writing.
c) File will not open and will show an error.
d) File will be created if it does not exist.
14. What will be the output if the code below is executed on an empty file
newfile.txt?
f = open('newfile.txt', 'r')
print(f.read())
f.close()
a) Blank output b) None c) 0 d) File not found error
15. Which function is used to delete a file in Python?
a) remove() b) delete() c) unlink() d) Both a and c
FILL UPS :
3. The _______ method reads all the lines from a file and returns them as a list.
4. In Python, the _______ method is used to read one line from a file.
5. The _______ function is used to close a file after it has been opened.
6. The _______ mode is used to open a file for reading only in Python.
7. In Python, to append data to a file without overwriting its existing content, the
file should be opened in _______ mode.
10. When a file is opened in _______ mode, it can be both read from and written
to.
11. If the file is opened in _______ mode, and it doesn't exist, Python will create a
new file.
12. The _______ method is used to remove the newline character from each line
when reading a file.
13. If a file is opened in _______ mode, it will be opened for reading and writing
in binary format.
14. The _______ method is used to move the file pointer to the beginning of the
file.
15. The _______ function is used to delete a file in Python.
Solution:
file = open("sample.txt", "r")
content = file.read()
words = content.split()
print(f"The number of words in the file is: {len(words)}")
file.close()
Explanation:
The file is opened in read mode ('r').
The content of the file is read using read().
The split() method splits the content into words (based on spaces), and len() is
used to count the number of words.
The file is closed after reading.
Solution:
file = open("sample.txt", "r")
content = file.read()
content = content.replace("Python", "Java")
file.close()
file = open("sample.txt", "w")
file.write(content)
file.close()
print("Word replaced successfully.")
Explanation:
The file is opened in read mode to read its content.
The replace() method is used to replace the word "Python" with "Java".
The file is reopened in write mode to overwrite the file with the modified
content.
Solution:
file = open("sample.txt", "r")
import os
if os.path.exists("sample.txt"):
print("The file 'sample.txt' exists.")
else:
print("The file 'sample.txt' does not exist.")
Explanation:
The os.path.exists() method is used to check whether the file exists or not.
If the file exists, it prints a message indicating its existence, otherwise, it prints
that the file does not exist.
Solution:
file = open("sample.txt", "r")
line_count = 0
for line in file:
line_count += 1
print(f"The number of lines in the file is: {line_count}")
file.close()