How to save a NumPy array to a text file? Last Updated : 26 Apr, 2025 Comments Improve Suggest changes Like Article Like Report When working with data it's important to know how to save NumPy arrays to text files for storage, sharing and further analysis. There are different ways from manual file handling to using specialized NumPy functions. In this article, we will see how to save a NumPy array to a text fileMethod 1: Using File Handling This method involves File handling a text file, converting the NumPy array to a string and writing it to the file using the write() function. After saving the array, file is closed using the close() function. Below are examples demonstrating this approach:Example 1: Saving a 1D Arrayfile = open("file1.txt", "w+"): Opens the file "file1.txt" in write mode and creates the file if it doesn't exist.file.write(content): Writes string representation of array to the file.file.close(): Closes the file after writing to it.file = open("file1.txt", "r"): Opens the file "file1.txt" in read mode to access its contents. Python import numpy List = [1, 2, 3, 4, 5] Array = numpy.array(List) print('Array:\n', Array) file = open("file1.txt", "w+") content = str(Array) file.write(content) file.close() file = open("file1.txt", "r") content = file.read() print("\nContent in file1.txt:\n", content) file.close() Output: 1D ArrayExample 2: Saving a 2D Array Python import numpy List = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Array = numpy.array(List) print('Array:\n', Array) file = open("file2.txt", "w+") content = str(Array) file.write(content) file.close() file = open("file2.txt", "r") content = file.read() print("\nContent in file2.txt:\n", content) file.close() Output: 2D ArrayMethod 2: Using numpy.savetxt()The numpy.savetxt() function is an efficient way to save NumPy arrays to text files. This method allows us to control formatting such as the number of decimals or delimiter between values. This function saves the data as floating-point values by default even when the original array contains integers.Example 1: Saving a 1D Arraynp.savetxt("file1.txt", array, fmt="%d"): Saves 1D array to a text file named file1.txt with the integer format (fmt="%d").content = np.loadtxt('file1.txt', dtype=int): Loads the contents of file1.txt back into a NumPy array as integers (dtype=int). Python import numpy as np array = np.array([1, 2, 3, 4, 5]) print('Array:\n', array) np.savetxt("file1.txt", array, fmt="%d") content = np.loadtxt('file1.txt', dtype=int) print("\nContent in file1.txt:\n", content) Output: 1D ArrayExample 2: Saving a 2D Array with Custom Delimitersnp.savetxt("file2.txt", array, delimiter=" ", fmt="%d"): Saves 2D array to a text file with space as the delimiter and integer format (fmt="%d").content = np.loadtxt('file2.txt', delimiter=" ", dtype=int): Loads content from file2.txt back into a NumPy array, interpreting data as integers (dtype=int) with space as the delimiter. Python import numpy as np array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print('Array:\n', array) np.savetxt("file2.txt", array, delimiter=" ", fmt="%d") content = np.loadtxt('file2.txt', delimiter=" ", dtype=int) print("\nContent in file2.txt:\n", content) Output: 2D ArrayWith these simple methods we can easily change our NumPy arrays to a text file as this approach helps us to manage our data more effectively and ensures it’s stored in a readable format. Comment More infoAdvertise with us Next Article How to save a NumPy array to a text file? R riturajsaha Follow Improve Article Tags : Python Python-numpy Python numpy-io Practice Tags : python Similar Reads NumPy save() Method | Save Array to a File The NumPy save() method is used to store the input array in a binary file with the 'npy extension' (.npy). Example: Python3 import numpy as np a = np.arange(5) np.save('array_file', a) SyntaxSyntax: numpy.save(file, arr, allow_pickle=True, fix_imports=True) Parameters: file: File or filename to whic 2 min read How to Convert an image to NumPy array and saveit to CSV file using Python? Let's see how to Convert an image to NumPy array and then save that array into CSV file in Python? First, we will learn about how to convert an image to a numpy ndarray. There are many methods to convert an image to ndarray, few of them are: Method 1: Using PIL and NumPy library. We will use PIL.Ima 4 min read How to Save Seaborn Plot to a File in Python? Seaborn provides a way to store the final output in different desired file formats like .png, .pdf, .tiff, .eps, etc. Let us see how to save the output graph to a specific file format. Saving a Seaborn Plot to a File in Python Import the inbuilt penguins dataset from seaborn package using the inbuil 2 min read How To Save Multiple Numpy Arrays NumPy is a powerful Python framework for numerical computing that supports massive, multi-dimensional arrays and matrices and offers a number of mathematical functions for modifying the arrays. It is an essential store for Python activities involving scientific computing, data analysis, and machine 3 min read How to convert NumPy array to list ? This article will guide you through the process of convert a NumPy array to a list in Python, employing various methods and providing detailed examples for better understanding. Convert NumPy Array to List There are various ways to convert NumPy Array to List here we are discussing some generally us 4 min read Like