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
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.
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,
- text files
- binary 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:
- Regular Text files
- These are the text files which store the text in the same form as we type.
- Here the newline character ends a line and the text translations take place.
- These files will have a file extension as .txt.
- Delimited Text files.
- 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.
- 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.
- 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.
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)