In Python, variables have a lifetime limited to the execution of the program. To store data permanently for future reusability, data must be written to secondary storage devices in the form of files. A file is a named location on a secondary storage medium where data is permanently stored.
Computers store all files as sequences of bytes (0s and 1s). However, files are classified based on how these bytes are interpreted:
\n
) or carriage return (
\r\n
). Common extensions include
.txt
,
.py
, and
.csv
.
.dat
,
.png
, and
.exe
.
Technical Profile: Data File Formats
.docx
file contains extensive metadata compared to a raw
.txt
file). Binary files are highly compact and fast but lack interoperability and are highly susceptible to corruption.
| Core Computer Science Concept | Technical Definition / Operational State | Structural Parameters & Specifications | Computational Function & System Purpose |
|---|---|---|---|
| Text File | A sequence of characters stored as encoded bytes. |
Uses EOL (
\n
) to demarcate lines; translated via ASCII/UNICODE.
|
Human-readable configuration, scripting, and structured data exchange. |
| Binary File | A raw stream of bytes representing direct memory states. | No line terminators; requires specific application-level parsers. | High-performance storage of multimedia, compiled code, and serialized objects. |
To perform any operation on a file, it must first be opened to establish a link between the program and the file on secondary storage. Once operations are complete, the file must be closed to free system resources.
The built-in open() function creates a file object (also known as a file handle) that acts as a pipeline for data transfer.
file_object = open(file_name, access_mode)
The file object exposes several key attributes:
file_object.closed
: Returns
True
if the file is closed,
False
otherwise.
file_object.mode
: Returns the access mode in which the file was opened.
file_object.name
: Returns the name of the file.
The access mode determines the permitted operations (reading, writing, appending) and the initial position of the file pointer (offset).
| File Mode | Description | File Offset Position |
|---|---|---|
<r>
|
Opens the file in read-only mode (default). Raises
FileNotFoundError
if the file does not exist.
|
Beginning of the file |
<rb>
|
Opens the file in binary and read-only mode. | Beginning of the file |
<r+>
or
<+r>
|
Opens the file in both read and write mode. | Beginning of the file |
<w>
|
Opens the file in write-only mode. Overwrites existing contents. Creates a new file if it does not exist. | Beginning of the file |
<wb+>
or
<+wb>
|
Opens the file in read, write, and binary mode. Overwrites existing contents. | Beginning of the file |
<a>
|
Opens the file in append mode. Appends data to the end of the file. Creates a new file if it does not exist. | End of the file |
<a+>
or
<+a>
|
Opens the file in append and read mode. | End of the file |
Closing a file is critical to flush unwritten data from the buffer to the disk and release system locks. This can be done explicitly using the close() method, or implicitly using the with statement (context manager).
# Explicit closing
file_object = open("myfile.txt", "r")
content = file_object.read()
file_object.close()
# Implicit closing using 'with' (Recommended)
with open("myfile.txt", "r") as file_object:
content = file_object.read()
Python provides distinct methods to read from and write to \text files, handling data either as single strings or sequences of strings.
str()
before writing.
# Example of writing to a file
with open("myfile.txt", "w") as f:
f.write("Hey I have started using files in Python\n")
lines = ["Hello everyone\n", "Writing multiline strings\n"]
f.writelines(lines)
n
characters from the file. If
n
is omitted or negative, it reads the entire file.
n
is specified, it reads at most
n
characters from the current line.
\n
).
# Example of reading from a file
with open("myfile.txt", "r") as f:
# Read first 10 characters
print(f.read(10))
# Reset pointer and read line-by-line
f.seek(0)
for line in f:
print(line.strip())
By default, file access is sequential. To access data in a random fashion, Python provides methods to query and manipulate the file pointer's position.
offset
bytes relative to the position specified by
reference_point
.
The
reference_point
parameter can take one of three values:
0
: Beginning of the file (default). Supported in both \text and binary modes.
1
: Current position of the file pointer. In \text mode, only an offset of
0
is allowed.
2
: End of the file. In \text mode, only an offset of
0
is allowed.
# Example of seek and tell
with open("testfile.txt", "r+") as f:
print("Initial position:", f.tell())
content = f.read(10)
print("Position after reading 10 bytes:", f.tell())
f.seek(0)
print("Position after seek(0):", f.tell())
Python objects (lists, dictionaries, custom class instances) exist only in memory during execution. To save these complex structures to a file and reconstruct them later, Python provides the pickle module.
Technical Profile: pickle Module
pickle.dump(obj, file_obj)
: Serializes
obj
and writes it to the binary file
file_obj
(must be opened in
wb
or
ab
mode).
pickle.load(file_obj)
: Reads a serialized byte stream from
file_obj
(must be opened in
rb
mode) and reconstructs the original Python object.
A system is required to accept employee records (ID, Name, Salary, Allowances) from a user, calculate total salary, write them to a binary file, and subsequently read and display all records.
Socio-Technical Analysis & Core Systemic Impact:
The implementation uses a binary file (
empfile.dat) opened in append-binary (ab) mode to store records as serialized lists. Reading is performed in awhile Trueloop inside atry...except EOFErrorblock, which gracefully handles the end-of-file condition whenpickle.load()attempts to read past the last record.import pickle # Writing records with open("empfile.dat", "wb") as bfile: record = [101, "D N Ravi", 32600, 4400, 37000] pickle.dump(record, bfile) # Reading records try: with open("empfile.dat", "rb") as bfile: while True: data = pickle.load(bfile) print("Record:", data) except EOFError: print("End of file reached successfully.")