File I/O: save, load, savetxt
NumPy file I/O is the set of functions that write arrays to disk and read them back — np.save and np.load for compact binary .npy files, np.savez for bundling several arrays into one .npz archive, and np.savetxt and np.loadtxt for human-readable CSV and text. In this lesson you'll round-trip arrays through binary and text formats, save multiple arrays at once, control the CSV delimiter and number format, and learn when each format is the right choice.
Learn File I/O: save, load, savetxt in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
np.save('name.npy', arr) writes an array to a compact binary file, and np.load('name.npy') reads it back. The .npy format stores the exact dtype and shape , so what you load is byte-for-byte identical to what you saved.
Expected output: the same 2×3 array, (2, 3) int64 , and True — the data round-trips perfectly.
Need to store several arrays together? np.savez('bundle.npz', x=a, y=b) packs them into one .npz archive. Loading returns a dictionary-like object; read each array back by the keyword name you gave it.
Expected output: ['x', 'y'] , then [0 1 2 3 4] and [1.5 2.5] .
When a human or another program (Excel, R, a database importer) must read the file, write text . np.savetxt('data.csv', arr, delimiter=',', fmt='%.2f') controls the separator and number format; np.loadtxt('data.csv', delimiter=',') reads it back into an array.
Handy savetxt options: delimiter=',' for CSV, fmt='%.2f' for two decimals, fmt='%d' for integers, and header='a,b,c' for a comment header line.
Expected output: the array reloaded as floats, with values rounded to 2 decimals, dtype float64 . (Note the tiny precision loss from text rounding.)
The choice comes down to who reads the file next . If it is your own NumPy program, use binary — it is faster, smaller, and lossless. If a person or a non-NumPy tool needs it, use text.
Expected output: the binary reload matches the original; the text reload is rounded to [0.33 0.67 0.14] .
Replace each ___ so the program saves an array to a binary file and loads it back unchanged.
Expected output: [ 88 92 79 100] then True . (Answers: save , load , loaded .)
❌ ValueError: could not convert string to float
np.loadtxt hit a header row or text it could not parse as a number.
✅ Fix: skip the header with skiprows=1 , or use np.genfromtxt for messy data.
❌ Wrong delimiter splits the file incorrectly
By default savetxt uses spaces; reading a comma file without delimiter=',' fails.
✅ Fix: pass the same delimiter to both savetxt and loadtxt.
Save a 2D integer table to a CSV with a header line, then load just the numbers back.
Lesson 20 complete — your arrays can live on disk!
You can now round-trip arrays through binary .npy and .npz files, write and read CSV text with custom delimiters and formats, and pick the right format for the job.
🚀 Up next: Performance — learn why vectorization beats Python loops and how to write fast code.
Practice quiz
What kind of file does np.save create?
- A compact binary .npy file
- A CSV text file
- A JSON file
- An Excel spreadsheet
Answer: A compact binary .npy file. np.save writes a binary .npy file that preserves dtype and shape exactly.
Which function reads a .npy file back into an array?
- np.read
- np.load
- np.open
- np.loadtxt
Answer: np.load. np.load reads .npy and .npz files created by np.save and np.savez.
How do you store several named arrays in one file?
- np.save
- np.savetxt
- np.savez
- np.dump
Answer: np.savez. np.savez('file.npz', a=arr1, b=arr2) bundles arrays into one .npz archive.
What does np.savetxt write?
- A binary .npy file
- A pickled object
- A compressed archive
- A human-readable text/CSV file
Answer: A human-readable text/CSV file. savetxt writes plain text, ideal when a human or another tool must read it.
After np.load('bundle.npz'), how do you read an array saved as x?
- x
Answer: x. An npz load returns a dict-like object; index it by the name you saved, data['x'].
What dtype does np.loadtxt return by default for a numeric CSV?
- int64
- object
- float64
- bool
Answer: float64. loadtxt parses values as float64 unless you specify a dtype.
Which savetxt argument sets the column separator for a CSV?
- sep=
- delimiter=
- split=
- comma=
Answer: delimiter=. delimiter=',' tells savetxt and loadtxt to use commas between values.
Why does np.save preserve data exactly while np.savetxt may not?
- save compresses better
- Binary .npy keeps full dtype and precision; text can round
- savetxt drops the array
- loadtxt always fails
Answer: Binary .npy keeps full dtype and precision; text can round. Binary stores exact bytes; text formatting like %.2f can lose precision.
How do you skip a header row when reading a CSV with np.loadtxt?
- header=True
- ignore=1
- skiprows=1
- skip=1
Answer: skiprows=1. skiprows=1 skips the first line; np.genfromtxt is also good for messy data.
When should you choose a text format over a binary .npy file?
- Always, text is faster
- When a person or a non-NumPy tool must read it
- When you need exact precision
- When saving many arrays
Answer: When a person or a non-NumPy tool must read it. Use text when humans or other programs read it; use binary for exact NumPy round-trips.