0% found this document useful (0 votes)
83 views5 pages

Xii Cs Text File Ws

This document contains a series of questions and tasks related to file handling in Python, including file opening modes, reading and writing functions, and specific programming exercises. It covers concepts such as text vs binary files, file operations like reading, writing, and closing files, as well as functions to manipulate and analyze file content. The document serves as a study material for students learning about file handling in computer science.

Uploaded by

mage
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)
83 views5 pages

Xii Cs Text File Ws

This document contains a series of questions and tasks related to file handling in Python, including file opening modes, reading and writing functions, and specific programming exercises. It covers concepts such as text vs binary files, file operations like reading, writing, and closing files, as well as functions to manipulate and analyze file content. The document serves as a study material for students learning about file handling in computer science.

Uploaded by

mage
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/ 5

CLASS XII

COMPUTER SCIENCE
WS 3 - FILE HANDLING - I
1. To open a file c:scores.txt for reading, we use a) Infile = open("c:\scores.txt", "T") b)
infile = open("c:\\scores.txt","r")
c) infile = open(file = "escores.txt", "T") d) infile = open(file = "c:\\scores.txt", "T")
2. To open a file c:\scores.txt for writing, we use a) outfile-open("c:\scores.txt", "w") b)
outfile-open("c:\\scores.txt", "w") c) outfile = open(file="c:\scores.txt", d) outfile-
open(file "c:\scores.txt", "w" 3. What is a file? What are the two basic opera on a file?
"w")
which are performed
4. What is the difference between a texti and a binary file?
5. Differentiate between "write" mod and "append" mode.
handsday.com
6. What different functions are vailable to write data in a text file? Describe each in
brief.
7. What different functions are available to read data from a text file? Describe each in
8. Why is it important to close a file?
9. In which mode should a file be opened: a) To write data into an existing file. b) To
read data from a file
To create a new file and write data into it.
To write data into an existing file. If the file does not exist, then file should be created
to write data into it.
10. Differentiate between seek() and tell().
11. Which functions are used to (i) rename a file, (i) delete a file? In which module are
these functions available?
12. What are the three standard input-output streams in Python?
13. Define the terms 'pickling" and "unpickling".
14. What are different file opening modes for a text file in Python? Write,
In brief, what does each mode do?
15. Consider the following code:
ch="A"
f-open("data.txt","a")
f.write(ch)
print(f.tell())
f.close()
What is the output if the file content before the execution program is the string
"ABC"?
16. Write a single statement to display the contents of the text file named "abc.txt"

17. Assuming that a text file named TEXT1.7


text file named already contains some text written into it, write a function named
vowelwords(), that reads the file TEXT1.TXT and creates a new file named TEXT2 TXT,
which shall contain only those words from the file TEXT1.TXT which don't start with
an uppercase vowel (Le., with 'A', 'E', 'T', '0', 'U'). For example, if the file TEXT.TXT
contains
Carry Umbrella and Overcoat When It rains
Then the text file TEXT2.TXT shall contain
Carry and When rains

18. Write a function in PYTHON to count and display the number of words starting
with alphabet 'A' or 'a' present in a text file "LINES.TXT". Example:
the file "LINES.TXT" contains the following lines,

A boy is playing there. There is a playground. An aeroplane is in the sky.


Are you getting it?

The function should display the output as 5.

19. Write a function show(n) that will display the nth character from the existing file
"MIRA.TXT". If the total characters present are less than n, then it should display
"invalid n".
20. Write a function CountHis Her() in PYTHON which reads the contents of a text file
"Gender.txt" which counts the words His and
Her (not case sensitive) present in the file.
For, example, if the file contains:
Pankaj has gone to his friend's house. His friend's name is
Ravya. Her house is 12KM from here.
The function should display the output:
Count of His: 2
Count of Her: 1
21 Write the function definition for WORD4CHAR() in PYTHON to read the content of a
text file FUN.TXT, and display all those words,
have four characters in it.
Example:
If the content of the file Fun.TXT is as follows:
When I was a small child, I used to play in the garden with my grand mom. Those days
were amazingly fun
all the moments of that time
and I remember
The function WORD4CHAR() should display the following: When used play with days
were that time

Page 1 of 3

1.Which mode is used to open a text file for reading in Python?


a) 'w'
b) 'r'
c) 'a'
d) 'x'

2.What does the readline() function return when the end of the file is reached?
a) EOF
b) -1
c) '' (empty string)
d) None

3.Which function is used to read all the lines of a file into a list?
a) read()
b) readlines()
c) readline()
d) readall()

4.Which method writes a list of strings to a file?


a) writelines()
b) writeall()
c) dump()
d) write()
5.Which of the following is a valid file object creation?
a) f = open("data.txt", "read")
b) f = open("data.txt", "write")
c) f = open("data.txt", "r")
d) f = file("data.txt", "r")

6.What will f.read(5) do?


a) Read first 5 lines
b) Read first 5 characters
c) Read next 5 words
d) Read first 5 bytes

7.What happens if you try to read a file that does not exist using 'r' mode?
a) File is created
b) Returns None
c) FileNotFoundError
d) Reads empty content

8.Which file mode allows both reading and writing without truncating the file?
a) 'w+'
b) 'r+'
c) 'a+'
d) Both b and c

9.Which function is used to close a file in Python?


a) file.close()
b) closefile()
c) f.close()
d) f.end()
10.Which method is used to check the current file position?
a) tell()
b) seek()
c) find()
d) locate()
11.Which of the following is used to move the file pointer to a specific position?
a) locate()
b) seek()
c) goto()
d) move()

12.What is the default mode of the open() function?


a) 'r'
b) 'w'
c) 'rb'
d) 'a'

13.What will f.write("Hello\nWorld") write into the file?


a) Hello World
b) Hello\nWorld
c) Hello
World
d) Error

14.Which mode is used to create a new file and open it for writing?
a) 'w'
b) 'a'
c) 'x'
d) 'r+'

1. Write a function in Python to count the number of lines in a text file ‘Country.txt’ which
start with the letter ‘W’ or ‘H’.

2. Write a user-defined function countwords() to display the total number of words in


“Quotes.txt”.

3. Write a function COUNT_AND() in Python to count the number of times “AND”


(including all cases like AND/and/And) occurs in “STORY.TXT”.

4. Write a function DISPLAYWORDS() to count words starting with “t” or “T” in a text file
“STORY.TXT”.

5. Write a function to count and display the number of 5-letter words in a text file
“Sample.txt”.

6. Write a function to display all lines starting with the letter “G” in “MyNotes.txt”.

7. Write a function in Python to read lines from “POEM.txt” and display only 2-character
words.

8. Write a function COUNT() to count occurrences of the word “Catholic” or “mother” in


“REPEATED.TXT”.
9. Write a method COUNTLINES_ET() to count lines in “REPORT.TXT” that start with
‘E’ and ‘T’ respectively.

10. Write a method SHOW_TODO() to display lines from “ABC.TXT” that contain the word
“TO” or “DO”.

11. Write a function that counts the number of “Me” or “My” words in “STORY.TXT”.

12. Write a function AMCount() to count the number of characters 'A'/'a' and 'M'/'m' in
“STORY.TXT”.

13. Write a function that displays the number of lines starting with ‘H’ in “para.txt”.

14. Write a function countmy() to count how many times “my” occurs in “Data.txt”.

15. Write a Python program to find the total number of lines in “abc.txt”.

16. Write a Python program to count how many times the word “if” occurs in “abc.txt”.

17. Write a method to display lines starting with the alphabet ‘P’ in “DIARY.TXT”.

18. Write a function ISTOUPCOUNT() to count how many times “IS”, “TO” or “UP” occurs
in “WRITER.TXT”.

19. Write a code in Python to count how many times “The” or “This” occurs in
“MY_TEXT_FILE.TXT”.

20. Write a function VowelCount() to count the occurrence of each vowel (A, E, I, O,
U) in “MY_TEXT_FILE.TXT”.

21. Write a function filter(oldfile, newfile) that copies lines from one file to
another, skipping lines that start with “@”.

You might also like