File Input and Output

Back

Loading concept...

NumPy File I/O: Your Data’s Safe House 🏠

Imagine you spent hours building the most amazing LEGO castle. What if you had to rebuild it every single time you wanted to play? That would be exhausting! Instead, you keep it safe on a shelf. NumPy arrays are like that castle—and File I/O is your shelf.


The Big Picture

When you create arrays in Python, they live in your computer’s memory (RAM). The moment you close your program—poof—they vanish like a dream you can’t remember.

File I/O (Input/Output) lets you:

  • Save arrays to files (so they don’t disappear)
  • Load them back whenever you need them

Think of it like a magic journal: write your array down, close the book, and open it years later—the array is still there, exactly as you left it.


Two Types of Storage Boxes

NumPy gives you two main ways to save arrays:

Type What It’s Like Best For
Binary A locked treasure chest Speed, big data, exact copies
Text A notebook anyone can read Sharing, simple data, human eyes

1. Saving Arrays to Binary Files

The Story

Binary files are like taking a photograph of your array. Every tiny detail is captured perfectly—the exact numbers, the shape, even the data type. It’s fast to take and fast to view.

How To Do It

import numpy as np

# Create an array
scores = np.array([95, 87, 92, 78, 88])

# Save it to a binary file
np.save('my_scores.npy', scores)

That’s it! NumPy creates a file called my_scores.npy. The .npy extension means “this is a NumPy binary file.”

Saving Multiple Arrays

What if you have several arrays? Use np.savez to put them all in one zip-like file:

names = np.array(['Alice', 'Bob', 'Charlie'])
ages = np.array([25, 30, 35])
heights = np.array([5.4, 6.0, 5.8])

# Save multiple arrays
np.savez('people.npz',
         names=names,
         ages=ages,
         heights=heights)

The .npz file is like a backpack—one bag holding many items.


2. Loading from Binary Files

The Story

Remember our photograph? Loading is like developing that photo and bringing your array back to life. NumPy reads every detail exactly as it was saved.

How To Do It

# Load a single array
loaded_scores = np.load('my_scores.npy')
print(loaded_scores)
# Output: [95 87 92 78 88]

Loading Multiple Arrays

# Load the npz file
data = np.load('people.npz')

# Access each array by its name
print(data['names'])   # ['Alice' 'Bob' 'Charlie']
print(data['ages'])    # [25 30 35]
print(data['heights']) # [5.4 6.0 5.8]

The loaded data acts like a dictionary—use the key name to get each array.

graph TD A["Your Array in Memory"] -->|np.save| B["Binary File .npy"] B -->|np.load| C["Array Restored!"] D["Multiple Arrays"] -->|np.savez| E["Archive .npz"] E -->|np.load| F["All Arrays Back"]

3. Saving Arrays to Text Files

The Story

Sometimes you want your data to be readable by humans (or other programs like Excel). Text files are like writing your array in a notebook with neat handwriting—anyone can open it and understand what’s inside.

How To Do It

import numpy as np

data = np.array([[1, 2, 3],
                 [4, 5, 6],
                 [7, 8, 9]])

# Save to text file
np.savetxt('my_data.txt', data)

Open my_data.txt in any text editor, and you’ll see:

1.000000e+00 2.000000e+00 3.000000e+00
4.000000e+00 5.000000e+00 6.000000e+00
7.000000e+00 8.000000e+00 9.000000e+00

Making It Prettier

# Save with nicer formatting
np.savetxt('my_data.csv', data,
           delimiter=',',    # Comma-separated
           fmt='%d',         # Integer format
           header='A,B,C')   # Column names

Now my_data.csv looks like:

# A,B,C
1,2,3
4,5,6
7,8,9

This file opens beautifully in Excel or Google Sheets!


4. Loading Arrays from Text Files

The Story

Loading from text is like having NumPy read your handwritten notebook and convert it back into an array. NumPy is a careful reader—it figures out the structure automatically.

How To Do It

# Load from text file
loaded = np.loadtxt('my_data.txt')
print(loaded)

Loading CSV Files

# Load CSV with comma delimiter
loaded_csv = np.loadtxt('my_data.csv',
                        delimiter=',',
                        skiprows=1)  # Skip header
print(loaded_csv)
# Output:
# [[1. 2. 3.]
#  [4. 5. 6.]
#  [7. 8. 9.]]

Handling Messy Data

Real-world data can be messy. Use np.genfromtxt for more control:

# Handle missing values
data = np.genfromtxt('messy.csv',
                     delimiter=',',
                     filling_values=0)

Binary vs Text: When to Use What?

graph TD Q["What do you need?"] --> A{Speed & Precision?} A -->|Yes| B["Use Binary .npy/.npz"] A -->|No| C{Human Readable?} C -->|Yes| D["Use Text .txt/.csv"] C -->|No| B B --> E["Fast, Exact, Compact"] D --> F["Shareable, Editable"]
Situation Choose
Saving for later use in Python Binary
Sharing with Excel users Text (CSV)
Very large datasets Binary
Need to edit by hand Text
Exact precision matters Binary

5. Memory-Mapped Files

The Story

Imagine you have a book with one million pages. Reading the whole thing would take forever and fill your entire room.

Memory-mapped files are like a magic window into that book. You only see the pages you need, when you need them. The book stays on the shelf, but you can read any page instantly.

Why Use Them?

  • Your array is too big to fit in memory
  • You want to access parts of a huge file without loading it all
  • Multiple programs need to read the same file at once

How To Do It

First, save a large array:

import numpy as np

# Create a large array
big_data = np.arange(1000000)
np.save('big_data.npy', big_data)

Now, access it with memory mapping:

# Memory-map the file
mapped = np.load('big_data.npy',
                 mmap_mode='r')

# Access just what you need
print(mapped[0:10])      # First 10 elements
print(mapped[999990:])   # Last 10 elements

Memory Map Modes

Mode Meaning Use Case
'r' Read only Viewing data safely
'r+' Read & write Modifying existing data
'w+' Write (creates new) Creating mapped arrays
'c' Copy-on-write Changes stay in memory only

Creating a Memory-Mapped Array

# Create a new memory-mapped array
fp = np.memmap('my_memmap.dat',
               dtype='float32',
               mode='w+',
               shape=(1000, 1000))

# Write to it like a normal array
fp[0, :] = np.arange(1000)
fp[500, 500] = 42.0

# Changes are saved automatically
del fp  # Flush changes
graph TD A["Huge File on Disk"] -->|Memory Map| B["Virtual Window"] B -->|Access| C["Only Load What You Need"] C --> D["RAM Stays Free"] D --> E["Work with Giant Data!"]

Quick Reference Table

Task Function Example
Save binary np.save() np.save('f.npy', arr)
Save multiple np.savez() np.savez('f.npz', a=arr1, b=arr2)
Load binary np.load() arr = np.load('f.npy')
Save text np.savetxt() np.savetxt('f.txt', arr)
Load text np.loadtxt() arr = np.loadtxt('f.txt')
Memory map np.load(mmap_mode=) arr = np.load('f.npy', mmap_mode='r')
Create memmap np.memmap() arr = np.memmap('f.dat', ...)

You Did It! 🎉

You now know how to:

  • Save arrays so they never disappear
  • Load them back exactly as they were
  • Choose between binary (fast & precise) and text (readable & shareable)
  • Handle massive files without running out of memory

Your data is safe. Your arrays are immortal. Go save the world—one .npy file at a time!

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.