0% found this document useful (0 votes)
703 views2 pages

Class 12 Python File Handling Q&A

This document discusses several examples of file handling in Python: 1. It shows how to create a file using the open() function in write mode, write text to it, and close it. 2. It demonstrates reading the contents of a file created in the previous example and printing the text. 3. It provides functions to count the number of lines in a file, display only the odd numbered lines, and write customer data entered by the user to a new file.

Uploaded by

Dominic Savio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
703 views2 pages

Class 12 Python File Handling Q&A

This document discusses several examples of file handling in Python: 1. It shows how to create a file using the open() function in write mode, write text to it, and close it. 2. It demonstrates reading the contents of a file created in the previous example and printing the text. 3. It provides functions to count the number of lines in a file, display only the odd numbered lines, and write customer data entered by the user to a new file.

Uploaded by

Dominic Savio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Conceptual Questions
  • Application-Based Questions
  • Additional Applications

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

1/6 
 
Data file handling in python class 12 important questions 
Data file handling in python class 12 Short answer question
2/6 
 
4. def display_oddLines(): 
f = open("friends.txt") cnt =0 
for lines in f: cnt+=1 
if cnt%2!=0: print(lines) 
f.close

You might also like