smart_open
Utils for streaming large files (S3, HDFS, gzip, bz2...)
A Python library that streams reading and writing of very large files from S3, HDFS, web URLs, or local disk, without loading the whole file into memory.
Smart_open is a Python library that makes it easy to read and write very large files stored in cloud services or on your computer, without having to load the entire file into memory at once. Instead of trying to pull a 10-gigabyte file all at once, smart_open streams it—reading and writing it in chunks, like water flowing through a pipe rather than filling a bucket.
The library works as a drop-in replacement for Python's built-in file opener. If you know how to use Python's open() function to read a text file, you already know the basic syntax for smart_open. The difference is that smart_open understands many more storage locations: Amazon S3 buckets, Hadoop file systems (HDFS), web URLs, SSH/SFTP servers, and local files. It also automatically handles decompression—if your file ends in .gz or .bz2, it decompresses transparently while you read. You write the same code whether you're reading from S3 or your laptop.
The problem it solves is real. Without smart_open, working with large remote files is tedious and error-prone. Cloud storage libraries like boto3 load everything into RAM by default, which fails for huge files. Smart_open abstracts away the messy details: multipart uploads, credential handling, network retries, and decompression logic all happen behind the scenes. A data scientist processing a terabyte dataset from S3 can loop through lines one by one with clean, readable code instead of wrestling with low-level storage APIs.
Anyone working with large datasets in the cloud benefits from this: data engineers building ETL pipelines, machine learning researchers training on remote data, or analytics teams querying big data warehouses. Even if you're just moving files around on a server or reading compressed backups, smart_open eliminates boilerplate. The README shows examples of everything from streaming S3 objects to piping data between compressed formats, all using identical, Pythonic syntax.
Where it fits
- Loop line by line through a multi-gigabyte file stored on S3 without loading it entirely into memory.
- Build an ETL pipeline that reads compressed .gz backups from cloud storage using the same code as reading local files.
- Stream a large dataset from HDFS or an SFTP server for a machine learning training job.
- Write to an S3 bucket in chunks using multipart upload without manually managing the low-level boto3 API.