File handling in python | text files in python | Binary files

what are files?

most of us are knowledgeable about working with files with our file explorer in our android phones and our pc too.

but can we do the same operations with code in python?

yes, we can do that, lets see how to do it?


Almost all computer programs work with files.

This is because files help us to store information permanently, and can be accessed and viewed

For example,

Word processors perform document files;

Database programs generate files of information;

Compilers translate source files and generate executable files.

So, we can see, it is the files that are mostly worked with, we can also work inside programs.

A file in itself collection of bytes saved on some storage device like a hard disk, pen drive, etc.

Every programming language offers some requirement to use and create files through programs.

Data files

A data file comprises the files that store data concerning a specific application, for later use. the data files can be stored in two different ways

Types of Data files

Depending upon the type of information stored in files it is classified into,

  1. text files
  2. binary files

Text Files

A text file stores information in the form of ASCII or Unicode characters (default one for our programming platform). 

In-text files, each line of text is terminated with a special character (according to Operating System) known as the EOL (End of Line) character.

In-text files, some internal adaptations take place when this EOL character is read or written.

Python, by default, the EOL character is the newline character (โ€˜\nโ€™) or carriage-return, newline combination (\r\n).

A text file can be classified into following types:

  1. Regular Text files
    1. These are the text files which store the text in the same form as we type.
    2. Here the newline character ends a line and the text translations take place.
    3. These files will have a file extension as .txt.
  2. Delimited Text files.
    1. In these text files, a specific character is stored to separate the values, ie after each value, for example a tab or a comme after every value.
    2. When a tab character is used to separate the values stored, these are called TSV files (Tab Separated Values files). These files can will have the extension as .txt or .csv.
    3. When the comma is used to separate the store the values ,then these files are called CSV files (Comma Separated Values files). These files will have the extension as .csv.

To understand more clearly,lets have a look at the following examples:

Regular text file Example:    This is simple text.

TSV file Example: This is simple text.

CSV file example: This,is,simple,text.

Important Point on CSV files:

The CSV (Comma Separated Values) arrangement is a popular import and export format for spreadsheets (MS EXCEL, GOOGLE SHEETS) and databases.

The most commonly used delimiter in a CSV file is a comma (,) but it can also use other delimiters characters like (tab), pipe (|), tilde(~).

Some setup files like .INI files and rich text format

files .RTF files are also text files.

Binary files

The text files can be opened by any text editor and they are also a human-readable format while binary files are not in human-readable form.

let’s have a brief discussion on Binary files (click here)

We will be happy to hear your thoughts

Leave a reply

Open chat
Scan the code
Can we help you?