How to Download and Extract a Dataset with keras.utils.get_file
Most deep-learning tutorials start by downloading a dataset, and nobody wants to download it again on every run. keras.utils.get_file downloads a file once, keeps it in a cache, extracts it if it's an archive, and checks it against a hash you give it. This guide downloads a small archive with Keras 3.15.1 and uses each of those features.
Syntax
keras.utils.get_file(
fname=<name>,
origin=<url>,
file_hash=<hash>,
extract=<True or False>,
cache_dir=<folder>,
cache_subdir=<subfolder>,
)
| Part | What it means |
|---|---|
origin | The web address to download from. The only argument you must give |
fname | Optional. The name to save it under. For an archive you extract, a name without a dot names the extracted folder |
file_hash | Optional. The file's expected SHA-256 (or MD5) hash. get_file checks the download against it |
extract | Optional. True unpacks a .zip, .tar, .tar.gz or .tar.bz archive and returns the folder |
cache_dir | Optional. Where to keep files. The default is ~/.keras, or the folder in the KERAS_HOME environment variable |
cache_subdir | Optional. A folder inside cache_dir. The default is datasets |
The Keras API documentation describes where files go: "By default the file at the url origin is downloaded to the cache_dir ~/.keras, placed in the cache_subdir datasets, and given the filename fname."
The Example
spa-eng.zip, a 2.6 MB archive of 118,964 English and Spanish sentence pairs from manythings.org/anki and the Tatoeba Project, released under CC BY 2.0 and hosted on TensorFlow's download server. The scripts in a uv project, get-file-demo, download it, find it in the cache, pin its hash and keep a copy inside the project.
Prerequisites
- A uv project with TensorFlow, which brings Keras with it. This guide uses Keras 3.15.1 and TensorFlow 2.22.0 on Python 3.14.10, with uv 0.13.10.
Step 1: Write the Script
import keras
path = keras.utils.get_file(
origin="https://storage.googleapis.com/download.tensorflow.org/data/spa-eng.zip",
extract=True,
)
print(path)

get_file returns a path. With extract=True, it's the path of the extracted folder.
Step 2: Run It
uv run python download.py
Downloading data from https://storage.googleapis.com/download.tensorflow.org/data/spa-eng.zip
2638744/2638744 ━━━━━━━━━━━━━━━━━━━━ 2s 1us/step
/Users/you/.keras/datasets/spa-eng.zip

get_file downloaded 2,638,744 bytes and extracted them. The returned path ends in spa-eng.zip, but it's a folder: with no fname, the extracted folder is named after the file. The uv run commands on this page work unchanged in PowerShell.
Step 3: Run It Again
uv run python download.py
/Users/you/.keras/datasets/spa-eng.zip

No download this time: the archive was already in the cache, so get_file used it and returned the same path.
Step 4: See What It Saved
List the cache folder, then the extracted folder:
- macOS / Linux
- Windows (PowerShell)
ls ~/.keras/datasets
Get-ChildItem $HOME/.keras/datasets -Name
spa-eng.zip spa-eng.zip_archive
- macOS / Linux
- Windows (PowerShell)
ls ~/.keras/datasets/spa-eng.zip
ls ~/.keras/datasets/spa-eng.zip/spa-eng
Get-ChildItem $HOME/.keras/datasets/spa-eng.zip -Name
Get-ChildItem $HOME/.keras/datasets/spa-eng.zip/spa-eng -Name
spa-eng
_about.txt spa.txt

get_file keeps two things:
| Entry | What it is |
|---|---|
spa-eng.zip_archive | The downloaded archive, saved with _archive added to its name. This is the file get_file looks for next time |
spa-eng.zip | The folder it extracted the archive into. Inside, the archive's own folder spa-eng holds the data, spa.txt, and a note about its source and license, _about.txt |
Step 5: Pin the File's Hash
A hash is a fingerprint of a file: if a single byte changes, the hash changes. The documentation suggests it: "Passing a hash will verify the file after download. The command line programs shasum and sha256sum can compute the hash." Compute the archive's SHA-256:
- macOS / Linux
- Windows (PowerShell)
shasum -a 256 ~/.keras/datasets/spa-eng.zip_archive
(Get-FileHash $HOME/.keras/datasets/spa-eng.zip_archive).Hash.ToLower()
0c4fd7825d3d872488d38c157f784e035edab4f2ff0df25a2a208790ecb5b58f /Users/you/.keras/datasets/spa-eng.zip_archive

The long hexadecimal number is the hash. PowerShell's Get-FileHash prints it in capital letters, and .ToLower() gives the same lowercase text that shasum prints.
Add the hash to the script:
import keras
path = keras.utils.get_file(
origin="https://storage.googleapis.com/download.tensorflow.org/data/spa-eng.zip",
file_hash="0c4fd7825d3d872488d38c157f784e035edab4f2ff0df25a2a208790ecb5b58f",
extract=True,
)
print(path)

uv run python download.py
/Users/you/.keras/datasets/spa-eng.zip

The cached archive matched the hash, so get_file used it. From now on, it checks the file every time the script runs, and a file that doesn't match is downloaded again.
Step 6: Name the Folder with fname
The folder name spa-eng.zip looks like a file. Give get_file a name without a dot, and it names the folder after it:
import keras
path = keras.utils.get_file(
fname="spa-eng",
origin="https://storage.googleapis.com/download.tensorflow.org/data/spa-eng.zip",
extract=True,
)
print(path)
- macOS / Linux
- Windows (PowerShell)
uv run python named.py
ls ~/.keras/datasets
uv run python named.py
Get-ChildItem $HOME/.keras/datasets -Name
Downloading data from https://storage.googleapis.com/download.tensorflow.org/data/spa-eng.zip
2638744/2638744 ━━━━━━━━━━━━━━━━━━━━ 1s 1us/step
/Users/you/.keras/datasets/spa-eng
spa-eng spa-eng.zip spa-eng.zip_archive spa-eng_archive

The folder is now spa-eng, with the archive beside it as spa-eng_archive. It was a new name, so get_file downloaded the archive again; the old pair stays until you delete it.
Step 7: Keep the Files in Your Project
The cache in your home folder is shared by every project. To keep a project's data next to its code, create a folder and pass it as cache_dir, with an empty cache_subdir so nothing is added under it:
from pathlib import Path
import keras
data = Path("data")
data.mkdir(exist_ok=True)
path = keras.utils.get_file(
origin="https://storage.googleapis.com/download.tensorflow.org/data/spa-eng.zip",
extract=True,
cache_dir=data,
cache_subdir="",
)
print(path)
- macOS / Linux
- Windows (PowerShell)
uv run python to_data.py
ls data
uv run python to_data.py
Get-ChildItem data -Name
Downloading data from https://storage.googleapis.com/download.tensorflow.org/data/spa-eng.zip
2638744/2638744 ━━━━━━━━━━━━━━━━━━━━ 1s 0us/step
data/spa-eng.zip
spa-eng.zip spa-eng.zip_archive

The same two entries, now in the project's data folder, and the returned path is relative to the project: data/spa-eng.zip. Add data/ to the project's .gitignore so the downloaded files never go into Git.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| A hash that doesn't match: a typo, or a file that changed on the server | get_file prints that the hash "does not match", downloads the file again, and then stops with ValueError: Incomplete or corrupted file detected. | Copy the hash again from shasum or Get-FileHash, or from the dataset's own published checksum |
Leaving out extract=True for an archive | get_file downloaded spa-eng.zip and returned the path of the .zip file itself, not of the data | Add extract=True |
FAQ
Is it safe to download archives this way?
Only from sources you trust. The documentation warns: "NEVER download a file/archive if you do not trust the source. We recommend that you specify the file_hash argument (if the hash of the source file is known) to make sure that the file you are getting is the one you expect."
How do I force a fresh download?
Pass force_download=True. The documentation: "If True, the file will always be re-downloaded regardless of the cache state."
Conclusion
keras.utils.get_file downloaded and extracted spa-eng.zip once, then returned it from the cache on every run after that. file_hash made it check the archive each time, fname named the extracted folder, and cache_dir kept a copy inside the project.
