0% found this document useful (0 votes)
47 views6 pages

File Handling in Python: Modes & Functions

Class 12th cbse board Computer science file handling viva questions practice sheet.
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)
47 views6 pages

File Handling in Python: Modes & Functions

Class 12th cbse board Computer science file handling viva questions practice sheet.
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

1. What is a file?

o A file is a stream of bytes stored on secondary storage devices


having an extension.
2. What are the different modes of opening a file?
o The different modes of opening a file are as follows:
 r,w,a,r+,w+,a+,rb,wb,ab,rb+,wb+,ab+
3. If no mode is specified in open() function, which mode will
be considered?
o r
4. What is the difference between “w” and “a” mode?
o “a” mode adds the content to the existing file whereas “w”
mode overwrites the contents into the file.
5. What is the difference between readline() and readlines()
function?
o readline() function reads the content of the text file and
returns the content into the string whereas readlines()
function reads the content of the text file and returns the
content into the list.
6. Parth wants to read only n number of characters from a text
file. Suggest him a function to accomplish his task.
o The read(n) function can be used
7. Nakshatra wants to count no. of words from the text file.
Suggest her code to accomplish a task.
o f=open(“[Link]”)
o w=[Link]().split()
o c=0
o for i in w:
 c+=1
 print(c)
8. What are the two different types of text files?
o Plain text or regular text files
o Delimited text files or separated text files
9. What are full forms of: a) csv b) tsv
o csv – comma separated values
o tsv – tab-separated values
10. Are CSV files and Text Files same?
o CSV files and Text Files are same in storage but csv file stores
the data separated by a delimiter.
11. What are the different valid delimiters?
o , is default delimiter
o other delimiters are tab – \t, colon – :, or semi colon – ;
12. What is pickling?
o Pickling refers to the process of converting python object
hierarchy into a byte stream to write into a binary file.
13. What is unpickling?
o It is the process of converting the byte stream back into an
object hierarchy.
14. Which module is required to handle binary files?
o pickle
15. Name the functions used to read and write data into
binary files.
o [Link](list_object, file_handle)
o [Link](file_object)
16. Which error is reported while reading the file binary file?
o ran out of input
17. How to avoid reading file errors in binary file?
o By using exception handling with try and except blocks
18. What is the significance of tell() and seek() functions?
o tell() function returns the current file position in a file
o seek() function change the current file position
19. What is the default value of offset for seek function?
o 0
20. What is offset in the syntax of seek function?
o Offset refers to the number bytes by which the file object is to
be moved.
21. Write about properties of a file.
o These are following properties of file :
 Name, mode, encoding, closed
 Name gives you name of file
 Mode gives you mode in which file is opened
 Encoding gives you encoding standard used by file
 Closed give you true if file is closed and false if file is
open
>>> f=open("[Link]")
>>> [Link]
'r'
>>> [Link]
'[Link]'
>>> [Link]
False
>>> [Link]
'cp1252'
>>>
22. Difference between file modes:

o
o
23. Difference between text file and binary file.

o
24. Difference between text file and CSV
o .txt File: This is a plain text file which can be opened using a
notepad present on all desktop PCs running MS Windows any
version. You can store any type of text in this file. There is no
limitation of what so ever text format. Due to ease of use for
end users many daily data summery providers use .txt files.
o .csv File: abreviation of "comma seperated values" This
is a special file extension commonly used by MS Excel.
Basically this is also a plain text file but with a limitation of
comma seperated values. Normally when you double click this
type of file it will open in MS Excel. These can be processed
fast in comparison to text file.
25. Difference between CSV and binary file

o
26. Difference between open and using with open.
o The with statement works with the open() function to open a
[Link], you can re-write the code we used in
the open() function example like this:
o with open("[Link]") as my_file:
print(my_file.read())

o # Output :
o # Hello world
o # I hope you're doing well today
Unlike open() where you have to close the file with
o
the close() method, the with statement closes the file for you
without you telling it to. This is because the with statement
calls 2 built-in methods behind the scene
– __enter()__ and __exit()__.The __exit()__ method closes the
file when the operation you specify is done.
PROGRAMS:
Text file:
Read
f=open("[Link]")
s1=[Link]()
print(s1)
[Link]()
write
f=open("[Link]",”w”)
[Link](“hello”)
[Link]()
wordwise:
def countword():
f=open("[Link]")
s=[Link]().split()
print(s)
n=0
for i in s:
if [Link]('A') or [Link]('a'):
n+=1
print("no of lines with vowel is",n)
[Link]()
Line wise:
def countline():
f=open("[Link]")
s=[Link]()
print(s)
n=0
for i in s:
if [Link]('A') or [Link]('a'):
n+=1
print("no of lines starting with vowel is",n)
[Link]()
CSV File
import csv
def accept():
f=open("[Link]",'a',newline="")
csvw=[Link](f,delimiter=",")
stdid=input("enter student id")
name=input("enter student b=name")
game=input("enter game")
result=input("enter result won, lost, tie")
data=[stdid,name,game, result]
[Link](data)
[Link]()

def woncount():
f=open("[Link]")
csvr=[Link](f)
c=0
for i in csvr:
if i[3]=="won":
c+=1
print(c)
[Link]()
binary file:
import pickle
def Add():
empid=int(input("enter employeeid"))
empname=input("enter employee name")
salary=int(input("enter salary"))
L1=[empid,empname,salary]
f=open("[Link]","ab")
[Link](L1,f)
[Link]()
def Search():
empid=int(input("enter empid"))
f=open("[Link]","rb")
while True:
try:
l1=[Link](f)
print(l1)
if l1[0]==empid:
print(l1)
except EOFError:
break

You might also like