Class 12 Computer Science - Text File Handling
(Q&A;)
Section A: 15 Text File Handling Q&A;
Q1. What is a text file in Python?
A text file is a file that contains characters readable by humans, stored with extension .txt. It is
opened using open('filename.txt','r/w/a').
Q2. Differentiate between text file and binary file.
Text file stores data in characters (ASCII/Unicode), while binary file stores data in raw byte format.
Q3. Which function is used to open a text file in Python?
The open() function is used with modes like 'r', 'w', 'a', 'r+'. Example: f=open('data.txt','r').
Q4. Explain read() function with example.
read() reads entire file content as string. Example: f=open('a.txt'); data=f.read(); f.close().
Q5. Explain readline() function.
It reads one line at a time from a file. Example: line=f.readline().
Q6. Explain readlines() function.
It returns all lines in a list. Example: lines=f.readlines().
Q7. What is the use of write() function?
write() writes a string to file. Example: f.write('Hello').
Q8. What is the use of writelines()?
It writes list of strings into file. Example: f.writelines(['A\n','B\n']).
Q9. What is the default mode of open()?
The default mode is 'r' (read mode).
Q10. What happens if we open a file in 'w' mode?
File content is erased and new content is written.
Q11. What is the use of 'a' mode?
'a' stands for append. It adds data at end without removing old data.
Q12. How to close a file?
Using close() method. Example: f.close().
Q13. What is tell() function?
It returns current file pointer position in bytes.
Q14. What is seek() function?
It changes file pointer position. Example: f.seek(0).
Q15. What is the importance of file handling?
It allows permanent storage of data, sharing, and retrieval when program ends.
Section B: 10 Three-Mark Q&A;
Q1. List any three modes of file opening with meaning.
'r' - read, 'w' - write, 'a' - append.
Q2. What is difference between read() and readlines()?
read() returns whole content as string, readlines() returns list of all lines.
Q3. Why is it necessary to close a file?
To free resources and ensure data is written correctly.
Q4. Write Python code to read first 5 characters of a file.
f=open('a.txt'); print(f.read(5)); f.close().
Q5. Differentiate between text file and binary file with example.
Text file: 'a.txt' (Hello), Binary file: 'a.dat' (byte data).
Q6. Write code to append 'Welcome' in a text file.
f=open('a.txt','a'); f.write('Welcome'); f.close().
Q7. Explain the use of 'r+' mode with example.
'r+' allows both reading and writing. Example: f=open('a.txt','r+').
Q8. What is the output of tell() if file is just opened?
It returns 0 because pointer is at beginning.
Q9. Write code to read a file line by line.
for line in open('a.txt'): print(line).
Q10. Mention two exceptions that may occur in file handling.
FileNotFoundError, IOError.
Section C: 10 Five-Mark Coding Q&A;
Q1. Write Python code to count number of lines in a file.
f=open('a.txt'); count=0 for line in f: count+=1 print(count); f.close()
Q2. Write Python code to count vowels in a file.
f=open('a.txt'); data=f.read().lower() c=0 for ch in data: if ch in 'aeiou': c+=1 print(c); f.close()
Q3. Write code to display all words of a file.
f=open('a.txt'); data=f.read().split() print(data); f.close()
Q4. Write program to copy one file to another.
f1=open('a.txt'); f2=open('b.txt','w') f2.write(f1.read()) f1.close(); f2.close()
Q5. Write program to search a word in file.
f=open('a.txt'); data=f.read() if 'Python' in data: print('Found') else: print('Not Found') f.close()
Q6. Write program to count frequency of each word.
f=open('a.txt'); words=f.read().split() d={} for w in words: d[w]=d.get(w,0)+1 print(d); f.close()
Q7. Write code to display file contents with line number.
f=open('a.txt'); n=1 for line in f: print(n,line); n+=1 f.close()
Q8. Write program to count characters, words and lines.
f=open('a.txt'); data=f.read() print(len(data), len(data.split()), data.count('\n')+1) f.close()
Q9. Write program to reverse content of file.
f=open('a.txt'); data=f.read()[::-1] print(data); f.close()
Q10. Write program to remove spaces from file content.
f=open('a.txt'); data=f.read().replace(' ','') print(data); f.close()
Section D: 15 Q&A; on seek() and tell()
Q1. What does tell() return?
It returns current file pointer position.
Q2. What is initial position of pointer?
0 at start.
Q3. After reading 5 chars, what will tell() return?
5.
Q4. How to move pointer to beginning?
f.seek(0).
Q5. How to move pointer to 10th byte?
f.seek(10).
Q6. Can we use negative value in seek()?
Yes with seek(offset,2) for end-based positioning.
Q7. What does seek(0,2) do?
Moves pointer to end of file.
Q8. What does seek(5,0) mean?
Moves to 5th byte from start.
Q9. What is difference between seek(0) and tell()?
seek(0) resets pointer, tell() shows position.
Q10. If file has 100 bytes, what is pointer after seek(50)?
50.
Q11. How to check if pointer is at end?
Compare tell() with file size using os.stat().st_size.
Q12. Why is seek() useful?
For random access of file content.
Q13. Give example of tell() usage.
pos=f.tell(); print(pos).
Q14. Can seek() move backward?
Yes, pointer can move to previous position.
Q15. Write code to go to middle of file.
import os f=open('a.txt'); size=os.stat('a.txt').st_size f.seek(size//2).