File Handling in Python - DATA Files Class 12 cbse computer science

File Handling in Python - DATA FILES

The data files are the files that store data pertaining to a specific application, for later use. The data files can be stored in two ways:

1.Text Files

2.Binary files


1.Text Files

A text file stores information in ASCII or Unicode characters (the one which is default for your programming platform). In text files, each line of text is terminated, (delimited) with a special character known as EOL (End of Line) character. In text files, some internal translations take place when this EOL character is read or written. In Python, by default, this EOL character is the newline character ('\n') or carriage-return, newline combination (\r\n').

2. Binary Files

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). In binary file, there is no delimiter for a line. Also no translations occur in binary files. As a result, binary files are faster and easier for a program to read and write than are text files. As long as the file doesn't need to be read by people or need to be ported to a different type of system, binary files are the best way to store program information.

OPENING AND CLOSING FILES

In order to work with a file from within a Python program, you need to open it in a specific mode as per the file manipulation task you want to perform. The most basic file manipulation tasks include adding, modifying or deleting data in a file, which in turn include any one or combination of the following operations :

  • reading data from files
  • appending data to files
  • writing data to files

Python provides built-in functions to perform each of these tasks. But before yo these functions on a file, you need to first open the file.

Post a Comment

If you any doubts, Please let me know

Previous Post Next Post