Introduction to Files Handling
A file is a collection of data stored on a storage device. It allows users and programs to
save, retrieve, and modify data efficiently. Files play a fundamental role in computing as
they store various types of information, such as text, images, videos, audio, and programs,
permanently.
Components of a File
A file consists of the following elements:
● File Name: A unique identifier assigned to the file.
● File Extension: Identifies the type of file (e.g., .txt, .jpg, .csv), helping the operating
system determine how to open it.
● File Path: The location of the file in a directory structure, which specifies where the file
is stored.
Types of Files
Files can be broadly categorized based on their format and usage:
1. Text Files
A text file is a file that contains only plain text characters, numbers, and symbols, with no
special formatting or binary data. It is human-readable and can be opened with simple
text editors like Notepad, VS Code, or any programming environment.
Characteristics of a Text File:
● Stores data as readable characters.
● Uses encoding formats like UTF-8 or ASCII.
● Each line is typically terminated by a newline character (\n).
● Can be edited using a basic text editor.
● File extensions: .txt, .html, .xml, .json, .csv, etc.
Examples:
● .txt – Stores simple text notes.
● .html – Contains webpage source code.
● .csv – Stores tabular data in plain text format.
2. Binary Files
Binary files are stored in terms of bytes (0s and 1s), but unlike text files, these bytes do not
represent the ASCII or Unicode values of characters. Rather, they represent the actual
content such as image, audio, video, compressed versions of other files, executable files,
etc. These files are not human readable. Thus, trying to open a binary file using a text editor
will show some garbage values. We need specific software to read or write the contents of
a binary file.
Characteristics of a Binary File:
● Stores data as raw binary values (bytes).
● Cannot be opened in a simple text editor.
● More efficient for storing large or complex data, such as images, audio, and
programs.
● File extensions: .exe, .jpg, .png, .mp3, .mp4, .bin, .dat, etc.
Examples:
● .jpg – Stores image data.
● .mp4 – Stores video data.
● .exe – Contains executable program files.
3. CSV (Comma-Separated Values) Files
A CSV file is a special type of text file used for storing tabular data in a plain-text format.
Each row in the file represents a record, and columns are separated by commas (,) or other
delimiters like semicolons (;).
Characteristics of a CSV File:
● Stores structured data in a simple and lightweight format.
● Can be opened in text editors, Microsoft Excel, or database programs.
● Commonly used for data exchange between applications.
● File extension: .csv.
Example of a CSV File:
Name, Age, City
John, 25, New York
Alice, 30, London
Mark, 22, Sydney
File Paths
A file path is the address or location of a file in a computer’s directory structure. There are
two types of file paths:
1. Absolute Path
An absolute path specifies the complete location of a file or directory from the root
directory. It includes all the directory levels leading to the file.
Example of an Absolute Path:
● Windows: C:\Users\John\Documents\[Link]
● Linux/macOS: /home/john/documents/[Link]
● Always starts from the root directory (C:\ in Windows, / in Linux).
● Works consistently regardless of the current directory.
● Used when you need to refer to a file from any location.
2. Relative Path
A relative path specifies the file location relative to the current working directory. It does not
include the root directory.
Example of a Relative Path:
● If the current directory is C:\Users\John\, then:
○ Documents\[Link] is a relative path to the file.
● In Linux, if the current directory is /home/john/, then:
○ documents/[Link] is a relative path.
● Shorter and more flexible than absolute paths.
● Depends on the current working directory.
● Useful when referring to files within the same project.
Key Differences Between Absolute and Relative Paths
Aspect Absolute Path Relative Path
Starting Point Root directory (/ or C:\) Current working directory
Portability Less portable (depends on More portable (works within the
system structure) same structure)
Length Longer Shorter
Use Case Accessing files from any Accessing files within the same
location project