Whether you’re building a small script or a large-scale application, you’ll need to store and retrieve data. Python offers multiple ways to handle data storage — from simple files to databases and cloud-based solutions.
In this guide, you’ll learn all the common methods for data storage in Python, including:
- Text and binary files
- JSON and CSV
- Pickle for object serialization
- SQLite (built-in database)
- External databases (MySQL, PostgreSQL)
- Cloud/file APIs (brief overview)
1. Storing Data in Text Files
Storing data in a .txt
file is useful for logs, settings, or simple data.
Write to File
with open("data.txt", "w") as file:
file.write("Hello, World!\n")
file.write("Welcome to Python.")
Read from File
with open("data.txt", "r") as file:
content = file.read()
print(content)
2. Storing Data in CSV Files
CSV (Comma-Separated Values) is ideal for tabular data (spreadsheets, logs, reports).
Writing to CSV
import csv
data = [
["Name", "Age"],
["Alice", 25],
["Bob", 30]
]
with open("data.csv", "w", newline="") as file:
writer = csv.writer(file)
writer.writerows(data)
Reading from CSV
with open("data.csv", "r") as file:
reader = csv.reader(file)
for row in reader:
print(row)
3. Storing Data in JSON Files
JSON is excellent for hierarchical or structured data (like dictionaries or API data).
Writing JSON
import json
data = {"name": "Alice", "age": 25, "skills": ["Python", "SQL"]}
with open("data.json", "w") as file:
json.dump(data, file)
Reading JSON
with open("data.json", "r") as file:
data = json.load(file)
print(data["name"])
4. Pickle: Store Python Objects Directly
pickle
allows you to serialize (save) Python objects (lists, classes, functions) in binary format.
Pickle Example
import pickle
my_data = {"username": "admin", "password": "1234"}
# Save
with open("data.pkl", "wb") as file:
pickle.dump(my_data, file)
# Load
with open("data.pkl", "rb") as file:
loaded_data = pickle.load(file)
print(loaded_data)
⚠️ Warning: Never unpickle data from untrusted sources. It can be a security risk.
5. Storing Data in SQLite (Built-in Database)
SQLite is a file-based database that’s lightweight and doesn’t require a server.
import sqlite3
# Connect to database (or create one)
conn = sqlite3.connect("mydata.db")
cursor = conn.cursor()
# Create table
cursor.execute("CREATE TABLE IF NOT EXISTS users (name TEXT, age INTEGER)")
# Insert data
cursor.execute("INSERT INTO users VALUES ('Alice', 25)")
conn.commit()
# Read data
cursor.execute("SELECT * FROM users")
print(cursor.fetchall())
conn.close()
6. External Databases (MySQL, PostgreSQL)
For larger or multi-user apps, use external databases.
- Use
mysql-connector-python
for MySQL: bashCopyEditpip install mysql-connector-python
- Use
psycopg2
for PostgreSQL: bashCopyEditpip install psycopg2
Example (MySQL):
import mysql.connector
conn = mysql.connector.connect(
host="localhost",
user="root",
password="yourpassword",
database="mydb"
)
cursor = conn.cursor()
cursor.execute("SELECT * FROM users")
print(cursor.fetchall())
conn.close()
7. Cloud Storage (Advanced)
For working with online storage:
- Google Drive →
pydrive
orgdown
- Firebase →
firebase-admin
- AWS S3 →
boto3
- Google Sheets →
gspread
These require authentication and are suitable for web apps, backups, and mobile integrations.
Summary: Best Python Data Storage Methods
Format | Best for | Pros | Limitations |
---|---|---|---|
.txt | Simple data | Easy to read/write | No structure |
.csv | Tables | Human-readable | Only rows/columns |
.json | Hierarchical data | Structured, widely used | No support for functions |
pickle | Python objects | Very flexible | Not human-readable, insecure |
SQLite | Small databases | Built-in, fast | Local use only |
MySQL/PostgreSQL | Scalable apps | Powerful and reliable | Needs setup |
Cloud (S3, Firebase) | Online apps | Accessible anywhere | Complex integration |
Final Thoughts
Choosing the right storage method depends on:
- What type of data you’re storing
- How much data you have
- Who/what will access the data
- Whether you need local or remote access