Class 12 Computer Science Chapter 2 Revision Summary Strictly NCERT

REVISION SUMMARY: File Handling in Python (NCERT Class 12)

1. Chapter at a Glance

  • A file is a named location on secondary storage media where data are permanently stored for later access.
  • Computers store every file as a collection of bytes (0s and 1s); there are two main types — text files and binary files.
  • Text files store human-readable characters (alphabets, numbers, special symbols) terminated by EOL (\n by default) and can be opened by any text editor.
  • Binary files store non-human-readable bytes representing images, audio, video, etc., and require specific software to access.
  • The open() function creates a file object (file handle) that links the program to the file for read/write operations; the file must be closed after use to free resources.
  • Data can be written using write() (single string) or writelines() (sequence of strings) and read using read(), readline() or readlines().
  • tell() returns the current byte position of the file object; seek() repositions it for random access using offset and reference point (0 = beginning, 1 = current, 2 = end).
  • The pickle module handles binary files by pickling (serializing) Python objects with dump() and unpickling (de-serializing) them with load().

2. Key Terms and Definitions

  • File: A named location on a secondary storage media where data are permanently stored for later access.
  • Text file: A sequence of characters consisting of alphabets, numbers and other special symbols; each line is terminated by the End of Line (EOL) character (default \n in Python); stored with extensions such as .txt, .py, .csv.
  • Binary file: A file stored as a stream of bytes that do not represent ASCII values of characters; contents (image, audio, video, etc.) are not human-readable.
  • EOL (End of Line): Special character that terminates each line of a text file (default newline \n); when encountered, the remaining contents are displayed from a new line.
  • file_object (file handle): Object returned by open() that establishes a link between the program and the data file; used to call read/write functions.
  • access_mode (processing mode): Argument to open() that specifies the operation (r = read, w = write, a = append, + = read+write, b = binary); default is read mode in text.
  • Pickling (serialization): Process of converting a Python object in memory into a byte stream for storage in a binary file.
  • Unpickling (de-serialization): Inverse process of converting a byte stream from a binary file back into a Python object.

3. Syntax and Constructs

Opening and closing a file

python file_object = open(file_name, access_mode) file_object.close() Example: myObject = open("myfile.txt", "a+"); myObject.close()

with clause (auto-closes file) python with open(file_name, access_mode) as file_object: Example: with open("myfile.txt","r+") as myObject: content = myObject.read()

write() method python file_object.write(string) Example: myobject.write("Hey I have started using files in Python\n")

writelines() method python file_object.writelines(iterable_of_strings) Example: myobject.writelines(["Hello everyone\n", "Writing multiline strings\n"])

read() method python file_object.read([n]) Example: myobject.read(10)

readline() method python file_object.readline([n]) Example: myobject.readline(10)

readlines() method python file_object.readlines() Example: myobject.readlines()

tell() method python file_object.tell() Example: fileobject.tell()

seek() method python file_object.seek(offset [, reference_point]) Example: fileobject.seek(10) # reference_point defaults to 0

pickle.dump() and pickle.load() python pickle.dump(data_object, file_object) store_object = pickle.load(file_object) Example: pickle.dump(listvalues, fileobject); objectvar = pickle.load(fileobject)

4. Algorithms and Worked Logic

Writing data to a text file (write/append mode) - Open file in 'w' (overwrites) or 'a' (appends) mode. - Accept string(s) from user; convert numeric data to string with str(). - Call write(string + '\n') or writelines(list_of_strings). - Close the file (or use with).

Reading a text file line-by-line (looping over file object) - Open in 'r', 'r+', 'w+' or 'a+' mode. - Use readline() inside a while loop: str = fileobject.readline(); while str: print(str); str = fileobject.readline(). - Stop when readline() returns empty string (EOF reached).

Random access using tell() and seek() - Call tell() to obtain current byte position. - Call seek(offset, 0/1/2) to move file object (0 = start, 1 = current, 2 = end). - Read from new position with read().

Pickling / unpickling records (binary file) - Import pickle. - Open in 'wb'/'ab' for dump or 'rb' for load. - pickle.dump(list_or_object, fileobject) to store; pickle.load(fileobject) to retrieve. - Handle EOFError with try-except when reading multiple records.

5. Common Errors and Exam Pitfalls

  • Forgetting to close the file after operations (memory not freed; data may remain in buffer).
  • Opening an existing file in 'w' mode instead of 'a' (previous contents overwritten).
  • Passing numeric data directly to write() without str() conversion.
  • Omitting \n at end of strings written with write() (lines not separated).
  • Using readlines() and expecting a single string instead of a list of strings ending with \n.
  • Incorrect reference_point value in seek() (defaults to 0; students often forget 0/1/2 meanings).
  • Not importing pickle before using dump()/load() on binary files.
  • Assuming writelines() returns number of characters written (only write() does).

A study aid reviewed by GFIS faculty — always verify with your textbook and teacher.