Data file handling in python class 12 important questions
Data file handling in python class 12 Short answer questions/
Conceptual Questions
Answers:
3. Python will creates a file automatically when open function is used with
write mode.
Example:
f=open("[Link]","w")
[Link]("Hello\nHow are you?")
[Link]()
Data file handling in python class 12 –Application Based questions
Answers:
1. # Creating file with open() function
f=open("[Link]","w")
[Link]("My city is very clean city.") [Link]()
# Reading contents from [Link] file
f=open("[Link]","r")
dt = [Link]() print(dt) [Link]()
2. Output:
Friends are honest
, Friends
are best!
Explanation:
In line no. 2, [Link]() function reads first line and stores the output string in l but
not printed in the code, then it moves the pointer to next line in the file. In next
statement we have [Link](18) which reads next 18 characters and place the
cursor at the next position i.e. comma (,) , in next statement [Link](10) reads next
10 characters and stores in ch3 variable and then cursor moves to the next position
and at last [Link]() function print() the entire line.
3. def count_lines():
f = open("[Link]") cnt =0
for lines in f: cnt+=1
print("no. of lines:",cnt) [Link]()
1/6
4. def display_oddLines():
f = open("[Link]") cnt =0
for lines in f: cnt+=1
if cnt%2!=0: print(lines)
[Link]()
5. def cust_data():
name = input("Enter customer name:")
age=int(input("Enter customer age:")) data =
str([name,age])
f = open("[Link]","w") [Link](data)
[Link]()
2/6