How to Save Array to File in Python

An array is a sequence of elements. In Python, we can save the array into a file and access that array in any other project. If the array is used in several projects, we don’t need to create the array in all the projects. Instead, we can save the array in a file and then access that array in all the projects where we use this array.

To work with an array in Python, use the numpy library. Numpy can be used to create a numpy array. This array can be saved in a file with the extension of .npy. The numpy array can be saved in the file and loaded when the array is used in the project.

How to Save Array to File in Python

To save an array to a file in Python, use the open() function to open a file and then use the file.write() function to write an array to a file and save that file with an extension of the .txt file.

arr = ["Hii ", "How ", "Are ", "You ",
       "I ", "Love ", "Python ", "Programming"]

with open("sample.txt", "w") as file:
   data = " ".join(arr)
   file.write(data)

Output

If you run the above file, It will create a sample.txt file with the following content.

Hii How Are You I Love Python Programming

You can see that we saved an array to the text file. So in this program, we created an array with some elements, and then we opened a text file named sample.txt in write mode to write the array, and then we joined the array and made the array as a string. And then, we have written that data into the file.

Using for loop to save the array to a text file

You can use the for loop instead of the join() to write the data into the sample.txt file.

arr = ["Hii ", "How ", "Are ", "You ",
       "I ", "Love ", "Python ", "Programming"]

with open("sample.txt", "w") as file:
  for line in arr:
     file.write(line)

Output

If you run the above file, It will create a sample.txt file with the following content.

Hii How Are You I Love Python Programming

Save an array to the .npy file in Python

The numpy.save() function saves an array to a binary file in NumPy .npy format. The numpy.asarray() converts the input to an array.

import numpy as np

arr = np.asarray([1, 2, 3, 4, 5, 6, 7, 8, 9])
np.save("sample.npy", arr)

Output

It will create a sample.npy binary file in your current working project directory.

In this program, we imported the numpy module. Numpy is a package mostly used for array creation and manipulation and mostly used in AI and Machine Learning tasks. This package provides an array called the numpy array. The numpy is very fast.

In the next line, we have created an array containing values from 1 to 9. This array is stored as a numpy array. It is then saved into a sample.npy file. This array is stored inside the sample.npy file.

How to load .npy file in Python

To load the .npy file in Python, use the np.load() function. The numpy.load() function loads arrays or pickled objects from .npy, .npz or pickled files. We can then load this array in other projects by simply loading the file. Load is the function for loading the npy file.

We already created a sample.npy file. Let’s load and read using the np.load() function.

import numpy as np

data = np.load( 'sample.npy' )
print(data)

Output

[1 2 3 4 5 6 7 8 9]

By using the np.load() function, we can load the stored numpy array. This file can be loaded into any project to access this array.

Conclusion

To save the numpy array to a text file in Python, use the combination of open() and write() functions. To save the numpy array to a .npy file in Python, use the np.save() file, and to read the .npy file, use the np.load() function.

That’s it for this tutorial.

See also

np.arange vs range

How to Write JSON File in Python

How to Read First Line of File in Python

Leave a Comment