Page15/20
File I/O & Reading Data Β· Page 1 of 1
Reading & Writing Text Files
File I/O & Reading Data
What is File I/O?
In computing, Input/Output (I/O) refers to all communication between a running program and the outside world β files on disk, network sockets, databases, or user input. File I/O specifically means reading data stored in files or writing results back to disk. Files are persistent: unlike variables in memory that disappear when the program ends, file data survives across runs.
As a data practitioner, virtually every real dataset you work with lives in a file β CSV, JSON, Excel, Parquet, or plain text. You must know how to read and write them reliably.
Reading Text Files
with open("data.txt", "r") as f:
content = f.read() # entire file as string
lines = f.readlines() # list of lines
The
withstatement: Automatically closes the file, even if an error occurs. Always use it!
Writing to Files
with open("output.txt", "w") as f:
f.write("Hello, Data Science!\\n")
f.writelines(["line1", "line2"]) # write multiple
Line-by-Line Processing (Memory Efficient)
with open("large_dataset.txt", "r") as f:
for line in f: # doesn't load entire file
process(line.strip())
main.py
Loading...
OUTPUT
βΆClick "Run Code" to executeβ¦