0% found this document useful (0 votes)
20 views4 pages

DFH WORKSHEET1

This document contains a worksheet for Class 12 C on Data File Handling in Python, featuring multiple-choice questions and fill-in-the-blank exercises related to file operations. It also includes programming problems with solutions for counting words, replacing text, reading lines, checking file existence, and counting lines in a file. The content is geared towards enhancing understanding of file handling concepts in Python.

Uploaded by

Seetha Devi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

DFH WORKSHEET1

This document contains a worksheet for Class 12 C on Data File Handling in Python, featuring multiple-choice questions and fill-in-the-blank exercises related to file operations. It also includes programming problems with solutions for counting words, replacing text, reading lines, checking file existence, and counting lines in a file. The content is geared towards enhancing understanding of file handling concepts in Python.

Uploaded by

Seetha Devi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

VELAMMAL BODHI CAMPUS – PUDUCHERRY

SENIOR SECONDARY COMPARTMENT


DATA FILE HANDLING - WORKSHEET
CLASS : 12 C SUB: COMPUTER SCIENCE

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()

6. Which of the following modes is used for appending to a file?


a) 'r' b) 'w' c) 'a' d) 'rb'
7. What will be the output of the following code if the file data.txt contains:
f = open('data.txt', 'r')
print(f.readline())
f.close()
a) 20 b) 10 c) 30 d) It will give an error.
8. Which function is used to close a file in Python?
a) end() b) close() c) stop() d) exit()
9. What does the 'r' mode in file handling do?
a) Opens the file for writing
b) Opens the file for reading
c) Opens the file for appending
d) Opens the file in binary mode
10. Which of the following is used to read all lines in a file at once?
a) read() b) readlines() c) readline() d) next()

11. What does the 'w' mode in file handling do?


a) Opens the file for writing, creates the file if it does not exist, and truncates the
file if it exists.
b) Opens the file for reading and writing.
c) Opens the file for appending.
d) Opens the file in binary mode for writing.

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 :

1. The _______ function is used to open a file in Python.


2. To write data into a file, the file must be opened in _______ mode.

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.

8. To create a new file for writing, the _______ mode is used.

9. The _______ method reads the entire content of a file at once.

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.

1. Program to count the number of words in a file.


Problem: Write a Python program to count the number of words in the file sample.txt.

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.

2. Program to find and replace a word in a file.


Problem: Write a Python program that will find and replace the word Python with Java
in the file sample.txt.

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.

3. Program to read file line by line.


Problem: Write a Python program to read and display each line of the file sample.txt
one by one.

Solution:
file = open("sample.txt", "r")

print("Reading the file line by line:")


for line in file:
print(line.strip()) # Strip the newline character
file.close()
Explanation:
 The file is opened in read mode ('r').
 The for loop is used to read the file line by line.
 The strip() method is used to remove the newline character from the end of each
line.
 The file is closed after reading.

4. Program to check if a file exists or not.


Problem: Write a Python program to check whether the file sample.txt exists or not.

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.

5. Program to count the number of lines in a file.


Problem: Write a Python program to count the number of lines in the file sample.txt.

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()

You might also like