Skip to main content

Text Files Processing

Text Files Processing in Python

Text files are used to store and read data, such as a simple text document or database output. Python offers various ways to work with Text Files, including reading, writing, and appending to files. Understanding how to handle text files is important for any programmer working in Python, as it can help them to efficiently manage and manipulate data within their program.

How to read text file in Python

To read a text file in Python, you can use the built-in function open() to open the file in read mode. Here are 2 code examples:

Open Text File

with open('filename.txt', 'r') as file:
    content = file.read()

Here, open() function opens the file filename.txt in read mode and returns a file object. The with statement is used to ensure that the file is properly closed after its use. read() function reads the contents of the file and stores it in the content variable.

Reading Text File Line by Line

with open('filename.txt', 'r') as file:
    for line in file:
        print(line)

Here, open() function opens the file filename.txt in read mode and returns a file object. The with statement is used to ensure that the file is properly closed after its use. for loop reads the file line by line and prints it on the console.

In both the examples, you can replace filename.txt with the path of the actual text file you want to read.

Write to a Text File in Python

Writing to a text file in Python is a basic file operation that involves creating a new file, writing data to it, and optionally closing the file. Here are two examples of how to write to a text file in Python:

file = open('example.txt', 'w')
file.write('Hello, world!')
file.close()

In this example, we create a new file called example.txt using the 'w' mode, which stands for write. Then, we use the write() method to add the string Hello, world! to the file. Finally, we close the file using the close() method.

file = open('example.txt', 'w')
lines = ['Line 1\n', 'Line 2\n', 'Line 3\n']
file.writelines(lines)
file.close()

Here, we create a new file called example.txt and assign it to the file variable with the w mode. Then, we define three lines of text as a list of strings and assign it to the lines variable. We use the writelines() method to write all the lines to the text file. Finally, we close the file using the close() method.

How to Get Python Output in Text File

If you want to save the output of your Python code into a text file, you can use the open() function in Python.

output = "Hello, world!"
with open("output.txt", "w") as file:
    file.write(output)

In this example, we have a string output that contains the text we want to save in the file. We open a file called output.txt in write mode using the open() function. Then, we use the write() method of the file object to save the contents of output into the file.

In either case, you can now open the file and see the text that was written. Although the examples are simple, you can modify them to suit more complex scenarios.

How to Convert Numpy Files to Text Files

When working with numerical data, numpy is one of the most popular libraries to manipulate arrays in Python. Sometimes, you may need to convert a numpy file to a text file for sharing and collaboration purposes. Here are two ways to convert a numpy file to a text file in Python:

The savetxt() function from the Numpy library can be used to save the data from an array to a text file. Here's how to do it:

### Method 1
import numpy as np

data = np.load('file.npy') # load the Numpy file
np.savetxt('file.txt', data) # save the data from the Numpy file to a text file

You can also use the tofile() method of the array object to save the data to a text file.

### Method 2
import numpy as np

data = np.load('file.npy') # load the Numpy file
data.tofile('file.txt', sep=',', format='%10.5f') # save the data to a text file

In the above example, the sep parameter specifies the separator between the data values (a comma in this case) and the format parameter specifies the format of the output values.

Contribute with us!

Do not hesitate to contribute to Python tutorials on GitHub: create a fork, update content and issue a pull request.

Profile picture for user AliaksandrSumich
Python engineer, expert in third-party web services integration.
Profile picture for user angarsky
Evgeniy Melnikovcontributor
Updated: 05/03/2024 - 21:52