Xii Cs Text File Ws
Xii Cs Text File Ws
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"
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,
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
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()
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
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’.
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.
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 “@”.