Ch-4 Data File Handling Preeti Arora Solution Computer Science

Here is the complete Preeti Arora Solution for Class 12 Computer Science  for Chapter Data File Handling

Unsolved Question From the Book Computer Science with Python written by Preeti Arora

Q 1.What is the difference between “w” and “a” modes?

Ans: The difference between “w” and “a” modes in file handling in Python is as follows:

  • “w” mode (write mode) is used for creating a new file or overwriting the existing file. If the file already exists, the previous content will be deleted and a new file will be created with the same name.
  • “a” mode (append mode) is used for appending data to an existing file. If the file doesn’t exist, a new file will be created. The data is added at the end of the file, preserving the existing content.

 

Q. 2 What is the significance of file-object?

Ans: The file-object in Python represents a file opened by the open() It provides methods and attributes to manipulate and interact with the file. The file-object allows reading, writing, and performing various file operations such as reading lines, writing data, moving the file pointer, and closing the file.

 

Q. 3 How are open() functions different from close() functions other than their functionality of opening and closing files?

Ans: The open() and close() functions in Python are used to perform file operations. The differences between them are as follows:

  • open() function: It is used to open a file and returns a file-object that can be used to perform various file operations like reading, writing, etc. The open() function is responsible for establishing a connection between the file and the program.
  • close() function: It is used to close an opened file. Once a file is closed using the close() function, no further operations can be performed on it. Closing a file is important to release system resources and ensure data integrity.

While their main functionality is opening and closing files respectively, the open() function is used to obtain a file-object to perform operations on the file, while the close() function is used to release system resources and ensure proper file handling.

 

Q. 4 Write statements to open a binary file C:\Myfiles\text1.txt in read and write mode by specifying the file path in two different formats.

Ans:

Statements to open a binary file, “C:\Myfiles\text1.txt,” in read and write mode using two different formats:

(a) Using single backslashes:

file = open(‘C:\\Myfiles\\text1.txt’, ‘rb+’)

(b) Using raw string:

file = open(r’C:\Myfiles\text1.txt’, ‘rb+’)

 

Q. 5 In which of the following file modes the existing data of the file will not be lost?

 

(a) ‘rb’

(b) ab

(c) w

(d) W+b

(e) ‘a+b’

(g) wb+

(f) wb

(i) r+

(h) w+

 

Ans: The file mode in which the existing data of the file will not be lost is:

(e) ‘a+b’ (append and binary mode) When a file is opened in this mode, data can be appended at the end of the file without losing the existing data.

 

Q. 6 What would be the data type of variable data in the following statements?

(a) Data = f.read.()

(b) data = f.read.(10)

(c) Data f.readline()

(d) data = f.readlines()

 

Ans: The data type of the variable “data” in the following statements would be:

(a) Data = f.read() – The data type of “data” would be a string. (b) data = f.read(10) – The data type of “data” would be a string. (c) Data = f.readline() – The data type of “data” would be a string. (d) data = f.readlines() – The data type of “data” would be a list of strings.

 

Q. 7 When a file is opened for output, what happens when

(a) The mentioned file does not exist

(b) The mentioned file exists?

 

Ans: When a file is opened for output:

(a) If the mentioned file does not exist, a new file with the specified name will be created.

(b) If the mentioned file exists, the existing file will be overwritten with the new data being written.

 

Q. 8 What role is played by file modes in file operations? Describe the various file mode constants and their meanings.

Ans: File modes in Python determine the type of operations that can be performed on a file. The various file mode constants and their meanings are:

  • ‘r’: Read mode. It is used for reading the contents of a file.
  • ‘w’: Write mode. It is used for writing data to a file. If the file already exists, it is truncated. If it doesn’t exist, a new file is created.
  • ‘a’: Append mode. It is used for appending data to an existing file. If the file doesn’t exist, a new file is created.
  • ‘x’: Exclusive creation mode. It is used for creating a new file but raises an error if the file already exists.
  • ‘b’: Binary mode. It is used for working with binary files.
  • ‘t’ or ‘text’: Text mode. It is used for working with text files.
  • ‘+’: Update mode. It allows both read and write operations on a file.

These file mode constants can be combined to define the desired behavior. For example, ‘rb’ represents read mode in binary, ‘w+’ represents write and read mode, ‘a+’ represents append and read mode, and so on.

 

Q. 9 Write a code snippet that will create an object called fileout for writing; associate it with the filename STRS. The code should keep on writing strings to it as long as the user wants.

 

Q. 10 Explain, how many file objects would you need to create to manage the following situations:

(a) To process three files sequentially

(b) To merge two sorted files into a third file.

 

Ans: The number of file objects needed to manage the following situations:

(a) To process three files sequentially: You would need three file objects, one for each file you want to process. Each file object can be opened individually and used for performing operations on each file.

(b) To merge two sorted files into a third file: You would need three file objects, two for the input files and one for the output file. The first two file objects would be opened in read mode to read the contents of the input files, and the third file object would be opened in write mode to write the merged data into the output file.

 

Data File Handling Preeti Arora Solution Computer Science

Q. 11 What are the advantages of saving data in:

(a) Binary form

(b) Text form?

 

Ans: Advantages of saving data in:

(a) Binary form: Saving data in binary form is advantageous when you want to preserve the exact representation of data and need to work with non-textual data. Binary files can efficiently store complex data structures, such as integers, floats, arrays, and custom objects. Binary data also takes up less storage space compared to text representation.

(b) Text form: Saving data in text form is advantageous when you want human-readable and editable data. Text files can be easily viewed, modified, and processed by both humans and machines. Text files are also platform-independent and can be easily shared and processed across different systems and programming languages.

 

Q. 12 When do you think text files should be preferred over binary files?

Ans: Text files should be preferred over binary files in the following situations:

  • When the data needs to be human-readable and editable.
  • When the data primarily consists of textual information.
  • When the data needs to be processed by programs that expect text input or produce text output.
  • When cross-platform compatibility and interoperability with other programming languages are required.
  • When the file size is not a major concern.

 

Q. 13 Write a program that reads a text file and creates another file that is identical except that every sequence of consecutive blank spaces is replaced by a single space.

 

Q. 14 A file sports.dat contains information in following format: Event-Participant. Write a function that would read contents from file sports.dat and creates a file named Atheletic.dat copying only those records from sports.dat where the event name is “Athletics”.

Q. 15 Write a program to count the words “to” and “the” present in a text file “Poem.txt”

 

Q. 16. Write a program to count the number of uppercase alphabets present in a text file “Poem.txt”

 

Q. 17 Write a program that copies one file to another. Have the program read the file names from user.

 

Q. 18 Write a program that appends the contents of one file to another. Have the program take the filenames from the user.

 

Q. 19 Write a program that reads characters from the keyboard one by one. All lower case characters get stored inside the file LOWER, all upper case characters get stored inside the file UPPER and all other characters get stored inside file OTHERS.

 

Q. 20 Write a program to search the names and addresses of persons having age more than 30 in the data list of persons.

 

Preeti Arora Solution Class 12 C.S. (083)

Q. 21 Write a function in Python to count and display the number of lines starting with alphabet “A” present in a text file “LINES.TXT”, e.g., the file “LINE.TXT” contains the following lines:

A boy is playing there.

There is a playground.

An aeroplane is in the sky.

 Alphabets & numbers are allowed in password.

The function should display the output as 3.

 

Q. 22 Write a function to insert a sentence in a text file, assuming that text file is very big and can’t fit in computer’s memory.

 

Q. 23 Write a program to read a file ‘Story.txt’ and create another file, storing an index of Story.txt, telling which line of the file each word appears in. If word appears more than once, then index should show all the line numbers containing the word.

 

Q. 24 Write a program to accept a filename from the user and display all the lines from the file which contain Python comment character ‘#”.

 

Q. 25 Reading a file line by line from beginning is a common task. What if you want to read a file backward? This happens when you need to read log files. Write a program to read and display content of file from end to beginning.

 

Q. 26 Write a Python program to display the size of a file after removing EOL characters, leading and trailing white spaces and blank lines.

 

Q. 27 Write a function Remove_Lowercase() that accepts two file names, and copies all lines that do not start with lowercase letter from the first file into the second file.

 

Q. 28 Write a program to display all the records in a file along with line/record number.

 

Q. 29 Write a method in Python to write multiple line of text contents into a text file mylife.txt.

 

Q.30. Write a method in Python to read the content from a text file DIARY.TXT line by line and display the same on the screen.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *