File Handling - Revision Notes

 CBSE Class 12 Computer Science (Python)

File Handling
Revision Notes

  • Some Important points for File Handling are:
  • A file in itself is a bunch of bytes stored on some storage devices like hard-disk, thumb-drive etc.
  • The data files can be stored in two ways:
    1. Text files
    2. Binary files
  • A text file stores information in ASCII or Unicode characters, where each line of text is terminated, (delimited) with a sepcial character known as EOL (End of Line) character. In text files some internal translations take place when this EOL character is read or written.
  • A binary file is just a file that contains information in the same format in which the information is held in memory, i.e., the file content that is returned to you is raw (with no translation or no specific encoding).
  • The open() function is used to open a data file in a program through a file-object (or a file-handle).
  • A file-mode governs the type of operations (e.g., read/ write/ append) possible in the opened file i.e., it refers to how the file will be used once it's opened.
  • A text file can be opened in these file modes: 'r', 'w', 'a', 'r+', 'w+', 'a+'
  • A binary file can be opened in these file modes: 'rb', 'wb', 'ab', 'r+b'('rb+'), 'w+b'('wb+'); a+b'('ab+').
  • The three file reading functions of Python are: read(), readline(), readlines()
  • While read() reads some bytes from the file and returns it as a string, readline() reads a line at a time and readlines() reads all the lines from the file and returns it in the form of a list.
  • The two writing functions for Python data files are write() and writelines().
  • While write() writes a string in file, writelines() writes a list in a file.
  • The input and output devices are implemented as files, also called standard streams.
  • There are three standard streams: stdin (standard input), stdout (standard output) and stderr (standard error)
  • The absolute paths are from the topmost level of the directory structure. The relative paths are relative to current working directory denoted as a dot(.) while its parent directory is denoted with two dots(..).
  • The full name of a file or a directory is called pathname.
  • Steps to Process a file: there are five steps to use files in the python program.
    1. Determine the type of file usage
      Under this step, you need to determine whether you need to open the file for reading purpose (input type of usage) or writing purpose (output type of usage)..
    2. Open the file and assign its reference to a file-object or file-handle
      Next, you need to open the file using open() and assign it to a file-handle on which all the file-operations will be performed. Just remember to open the file in the file-mode that you decided in step 1.
    3. Now process as required
      As per the situation, you need to write instructions to process the file as desired. For example, you might need to open the file and then read it one line at a time while making some computation, and so on.
    4. Close the file
      This is very important step especially if you have opened the file in write mode. This is because, sometimes the last lap of data remains in buffer and is not pushed on to disk until a close() operation is performed.