Data file handling in python class 12 you get important Questions for File Handling – Computer Science with Python. For answers check out these posts:
- File Handling – Text Files
- Binary Files
- Binary file operations
Topics Covered
Data file handling in python class 12 Objective type questions:
Fill in the blanks
- A collection of bytes stored in computer’s secondary memory is known as _______.
- ___________ is a process of storing data into files and allows to performs various tasks such as read, write, append, search and modify in files.
- The transfer of data from program to memory (RAM) to permanent storage device (hard disk) and vice versa are known as __________.
- A _______ is a file that stores data in a specific format on secondary storage devices.
- In ___________ files each line terminates with EOL or ‘\n’ or carriage return, or ‘\r\n’.
- To open file data.txt for reading, open function will be written as f = _______.
- To open file data.txt for writing, open function will be written as f = ________.
- In f=open(“data.txt”,”w”), f refers to ________.
- To close file in a program _______ function is used.
- A __________ function reads first 15 characters of file.
- A _________ function reads most n bytes and returns the read bytes in the form of a string.
- A _________ function reads all lines from the file.
- A _______ function requires a string (File_Path) as parameter to write in the file.
- A _____ function requires a sequence of lines, lists, tuples etc. to write data into file.
- To add data into an existing file ________ mode is used.
- A _________ function is used to write contents of buffer onto storage.
- A text file stores data in _________ or _________ form.
- A ___________ is plain text file which contains list of data in tabular form.
- You can create a file using _________ function in python.
- A __________ symbol is used to perform reading as well as writing on files in python.
Find answer below for data file handling in python class 12.
Answers:
- File
- File Handling
- I/O Operations
- Data file
- Text File
- open(“data.txt”,”r”)
- open(“data.txt”,”w”)
- File handle or File Object
- close
- read(15)
- readline()
- readlines()
- write()
- writelines()
- append
- flush()
- ASCII, UNICODE
- CSV
- open()
- +
Data file handling in python class 12 – MCQs
1 Every file has its own identity associated with it. Which is known as –
a. icon
b. extension
c. format
d. file type
2 Which of the following is not a known file type?
a. .pdf
b. jpg
c. mp3
d. txp
3. In f=open(“data.txt”, “r”), r refers to __________.
a. File handle
b. File object
c. File Mode
d Buffer
4. EOL stands for
a. End Of Line
b. End Of List
c. End of Lines
d. End Of Location
5. Which of the following file types allows to store large data files in the computer memory?
a. Text Files
b. Binary Files
c. CSV Files
d. None of these
6. Which of the following file types can be opened with Notepad as well as MS Excel as well as MS Word?
a. Text Files
b. Binary Files
c. CSV Files
d. None of these
7. Which of the following is not a proper file access mode?
a. close
b. read
c. write
d. append
8. To read the 4th line from the text file, which of the following statements is true?
a. dt = f.readlines();print(dt[3])
b. dt=f.read(4) ;print(dt[3])
c. dt=f.readline(4);print(dt[3])
d. All of these
9 Which of the following function flushes the files implicitly?
a. flush()
b. close()
c. open()
d. fflush()
10. Which of the following functions flushes the data before closing the file?
a. flush()
b. close()
c. open()
d. fflush()
Data file handling in python class 12 – Short answer questions/ Conceptual Questions
Please refer notes section for the answers on Data file handling in python class 12.
- What do you mean by file? What do you mean by file handling?
- The file refers to the collection of bytes stored in computer storage.
- Data can be stored in various forms in a file.
- These files saved in a specific format with a specific extension.
- Every file needs to have a specific program to read them.
- Fille handling refers to the process of handling data using software for IO operations.
- Explain open() function with its syntax in detail.
- The open function has the following syntax:
- Open a text file: Syntax:
= open(file_name,access_mode) - file object : It is just like a variable or object
- open(): It is a function with two parameters.
- file_name: It accepts a file name with .txt extension.
- access_mode: It specifies the mode to access the file. The default mode is reading mode.
- These modes are
- r: to read a file
- w: to write
- a: append contents
- These modes are
- Does python create itself if the file doesn’t exist in the memory? Illustrate your answer with an example.
- Python will create a file automatically when the open function is used with write mode.
- Example:
- f=open(“data.txt”,”w”)
- f.write(“Hello\nHow are you?”)
- f.close()
- Write a statement to create a data.txt file with thefollowing text.
- Python file handling is very interesting and useful.
- This is a text file created through python.
- f=open(“data.txt”,”w”)
- f.write(“Python file handling is very interesting and useful.”)
- f.write(“This is a text file created through python.”)
- f.close()
- List out the basic file modes available in python.
- r – to read from the file
- w – to write into the file
- a – append data into the file already exists
- r+/w+ – to perform read and write together
- rb/wb/ab – read, write and append data into binary files
- Compare text files, binary files and csv files and write pros and cons of each of them.
Text Files | Binary Files | CSV Files | |
1 | It is capable to handle textual data. | It is capable to handle large file. | It is very common format and platform independent. |
2 | It consists of series of lines of a set of letters, numbers or symbols (String) | It consists of data with a specific pattern without any delimiter. | It consists of plain text with a list of data with a delimiter. |
3 | Any text editors like notepad can be used to read them. | No specific programs can be used to read them, python provides functions to read data. | It can be read using text editors like notepads and spreadsheet software. |
4 | Every line ends with EOL. | There is no specific EOL character. | It terminates a line automatically when the delimiter is not used after data. |
Data file handling in python class 12 – Application-Based questions
The following section contains a few case study-based questions for Data file handling in Python class 12.
- Write a python program to create and read the city.txt file in one go and print the contents on the output screen.
Answer:
# Creating file with open() function
f=open("city.txt","w")
f.write("My city is very clean city.")
f.close()
# Reading contents from city.txt file
f=open("city.txt","r")
dt = f.read()
print(dt)
f.close()
2. Consider following lines for the file friends.txt and predict the output:
Friends are crazy, Friends are naughty ! Friends are honest, Friends are best ! Friends are like keygen, friends are like license key ! We are nothing without friends, Life is not possible without friends !
f = open("friends.txt")
l = f.readline()
l2 = f.readline(18)
ch3=f.read(10)
print(l2)
print(ch3)
print(f.readline())
f.close()
Output:
Friends are honest
, Friends
are best !
Explanation:
In line no. 2, f.readline() 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 f.readline(18) which reads next 18 characters and place the cursor at the next position i.e. comma (,) , in next statement f.read(10) reads next 10 characters and stores in ch3 variable and then cursor moves to the next position and at last f.readline() function print() the entire line.
3. Write a function count_lines() to count and display the total number of lines from the file. Consider above file – friends.txt.
def count_lines():
f = open("friends.txt")
cnt =0
for lines in f:
cnt+=1
print("no. of lines:",cnt)
f.close()
4. Write a function display_oddLines() to display odd number lines from the text file. Consider above file – friends.txt.
def display_oddLines():
f = open("friends.txt")
cnt =0
for lines in f:
cnt+=1
if cnt%2!=0:
print(lines)
f.close()
5. Write a function cust_data() to ask user to enter their names and age to store data in customer.txt file.
def cust_data():
name = input("Enter customer name:")
age=int(input("Enter customer age:"))
data = str([name,age])
f = open("customer.txt","w")
f.write(data)
f.close()
Thank you very much for visiting our website and reading the article data file handling in python class 12.
Click here to read data file handling in python class 12 notes for text files.
Kindly share the contents to help the community and your friends. Share your views in the comment section about this article.
Feel free to ask your doubts in the comments section as well.
If you are looking for Sumita Arora textbook solutions on Data file handling in python class 12 click here.
If you need more questions and assignments let us know in the comments on the topic Data file handling in python class 12.
Download the contents in pdf file.
great job
Thank you
Great Job. Please add more Case study questions