Skip to main content

How to Download and Extract a Dataset with keras.utils.get_file

· 10 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
keras.utils.get_file(
fname=<name>,
origin=<url>,
file_hash=<hash>,
extract=<True or False>,
cache_dir=<folder>,
cache_subdir=<subfolder>,
)
PartWhat it means
originThe web address to download from. The only argument you must give
fnameOptional. The name to save it under. For an archive you extract, a name without a dot names the extracted folder
file_hashOptional. The file's expected SHA-256 (or MD5) hash. get_file checks the download against it
extractOptional. True unpacks a .zip, .tar, .tar.gz or .tar.bz archive and returns the folder
cache_dirOptional. Where to keep files. The default is ~/.keras, or the folder in the KERAS_HOME environment variable
cache_subdirOptional. 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​

download.py
import keras

path = keras.utils.get_file(
origin="https://storage.googleapis.com/download.tensorflow.org/data/spa-eng.zip",
extract=True,
)
print(path)

The download.py script in a macOS-style code window: it imports keras, calls keras.utils.get_file with the spa-eng.zip address as origin and extract=True, and prints the returned path.

get_file returns a path. With extract=True, it's the path of the extracted folder.

Step 2: Run It​

Terminal
uv run python download.py
Output
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

The terminal after uv run python download.py: Downloading data from the spa-eng.zip address, a progress bar showing 2638744 of 2638744 bytes in 2 seconds, and the path /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​

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

The terminal after running download.py a second time: only the path /Users/you/.keras/datasets/spa-eng.zip, with no download.

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:

Terminal
ls ~/.keras/datasets
Output (macOS)
spa-eng.zip spa-eng.zip_archive
Terminal
ls ~/.keras/datasets/spa-eng.zip
ls ~/.keras/datasets/spa-eng.zip/spa-eng
Output (macOS)
spa-eng
_about.txt spa.txt

The terminal after listing the cache: ~/.keras/datasets holds spa-eng.zip and spa-eng.zip_archive, spa-eng.zip holds a folder spa-eng, and that folder holds _about.txt and spa.txt.

get_file keeps two things:

EntryWhat it is
spa-eng.zip_archiveThe downloaded archive, saved with _archive added to its name. This is the file get_file looks for next time
spa-eng.zipThe 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:

Terminal
shasum -a 256 ~/.keras/datasets/spa-eng.zip_archive
Output (macOS)
0c4fd7825d3d872488d38c157f784e035edab4f2ff0df25a2a208790ecb5b58f /Users/you/.keras/datasets/spa-eng.zip_archive

The terminal after shasum -a 256 on spa-eng.zip_archive: the hash 0c4fd7825d3d872488d38c157f784e035edab4f2ff0df25a2a208790ecb5b58f followed by the file's path.

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:

download.py
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)

The download.py script with file_hash added in a macOS-style code window: the same get_file call, now with file_hash set to 0c4fd7825d3d872488d38c157f784e035edab4f2ff0df25a2a208790ecb5b58f.

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

The terminal after running download.py with file_hash: only the path /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:

named.py
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)
Terminal
uv run python named.py
ls ~/.keras/datasets
Output (macOS)
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 terminal after running named.py and listing the cache: a new download, the path /Users/you/.keras/datasets/spa-eng, and four entries in the cache: spa-eng, spa-eng.zip, spa-eng.zip_archive and 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:

to_data.py
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)
Terminal
uv run python to_data.py
ls data
Output (macOS)
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 terminal after running to_data.py and listing data: a download, the path data/spa-eng.zip, and the data folder holding spa-eng.zip and 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​

MistakeWhat happensDo this instead
A hash that doesn't match: a typo, or a file that changed on the serverget_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 archiveget_file downloaded spa-eng.zip and returned the path of the .zip file itself, not of the dataAdd 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.

Additional Resources​

  • get_file — the Keras API documentation.
  • Tatoeba — where the example's sentence pairs come from.