1/11
By the end of this section, students should be able to... • Remember the role of data I/O in programming • Know the basic I/O operations required to write/read data • Differentiate between ASCII and binary file formats (review) • Perform basic I/O operations in Python: • Read/write ASCII and binary data • Handle ‘csv’ files using csv and pandas • Choose file formats based on readability, performance, and use case
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Data I/O
The process of reading from or writing to files, enabling interaction between programs and external data.
ASCII File
Human-readable format (e.g., .txt
, .csv
); easier to debug but slower and larger in size.
Binary File
Machine-readable format (e.g., .npy
, .npz
); faster and smaller but not human-readable.
Use of ASCII vs. Binary
Choose ASCII for debugging and transparency, binary for speed and compact storage.
Basic File Access
Files must be opened before use and closed after to save data and free system resources.
open(fname, access_type)
Opens a file with name fname
and operation type ('r'
, 'w'
, etc.). Returns a file object (fid).
File ID (fid)
A unique identifier Python uses to reference an open file.
File Access Modes
'r'
: read, 'w'
: write, 'a'
: append, 'r+'
: read/write, 'rb'
: read binary, etc.
Closing a File
Use fid.close()
to ensure data integrity and prevent memory/resource leaks.
CSV Handling
Use csv
(standard module) or pandas
(powerful data analysis library) to read/write structured tabular data.
Choosing File Format
Depends on readability (ASCII), performance (binary), or data structure (CSV, JSON, etc.).
MATLAB vs. Python I/O
Both platforms support ASCII/binary operations but with different syntax and ecosystem preferences.