SoFunction
Updated on 2025-03-03

Detailed tutorial on processing and manipulating plain text files in Python 3

Introduction

Python is a powerful tool for processing data. One of the most common tasks in programming is to read, write, or manipulate data. Therefore, it is particularly important to understand how to handle different file formats that store different types of data.

For example, consider a Python program that checks a user's access control list. Your user list may be stored in a text file that allows you to check access or modify permissions. With Python, being able to open, read, write, and close files will help with such tasks.

This tutorial will briefly describe some file formats that Python can handle. After briefly introducing these file formats, you will learn how to open, read, and write text files in Python 3. Once done, you will be able to process any plain text file in Python.

Prerequisites

Before proceeding with this tutorial, you should have Python 3 installed and have a local programming environment set up on your computer. If you haven't done this, you can follow the appropriate installation and setup guide for your operating system:

  • Ubuntu 22.04 or Debian 8
  • CentOS 7
  • Mac OS X
  • Windows 10

background

Python is very flexible and can easily handle a variety of different file formats, including but not limited to the following:

File Type describe
Plain text Plain text files store data that contains only characters (or strings) and exclude any structured metadata
CSV Comma-separated value files use commas (or other separators) to structure stored data, allowing data to be saved in tabular format
HTML Hypertext markup language file storage for structured data displayed by the browser, usually used with websites
JSON JavaScript object notation is a simple and efficient format that makes it one of the most commonly used formats for storing and transmitting structured data.

This tutorial will focus on how to handle plain text files.

Step 1 - Create a text file

Before we start working in Python, we need to make sure there is a file to process. To do this, please open your code editor and create a name calledNew plain text file.

In the new file, enter a few lines of text to list the days of the week:

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

Next, save your file and note its location. In this case, our userssammySave the file in/home/sammy/. This will be very important in the next steps, as we will open the file in Python.

Now that you have the files to process, you can start writing code.

Step 2 ——Open the file

In your code editor, create a new Python file and name it

To open a file in Python, we first need a way to associate files on disk with variables in Python. This process is called opening a file, while a variable is called a file handle. We first tell the location of the Python file. The location of a file is often referred to as the file path—in this case /home/sammy/. Create a variable to store this path information.

path = '/home/sammy/'

Now you can use Python's open() function to open our file. The open() function requires the file path as its first parameter. This function also accepts many other parameters. However, the most important thing is the optional mode parameter. This is an optional string that specifies the mode to open the file. The pattern you choose will depend on what you want to do with the file. Here are some available patterns:

  • 'r': Used to read data from a file
  • 'w': Used to write data to a file
  • 'a': Used to append data to a file
  • 'r+': Used to read and write in the same file

In this case we just want to read data from the file, so we will use'r'model. useopen()Function openfile and assign the generated file handle to the variabledays_file

days_file = open(path, 'r')

Now that you have opened the file, the next steps will guide you to read its contents.

Step 3 — Read the file

Since we have opened the file, we can now manipulate it (i.e. read it from it) by the variables assigned to it. Python provides three related operations to read information from a file. Let's take a moment to see how they each work.

Use read

The first operationread()Returns the contents of the entire file as a string. For example:

days_file.read()

The result will be:

'Monday\nTuesday\nWednesday\nThursday\nFriday\nSaturday\nSunday\n'

Use readline

The second operationreadline()Returns the next line of the file, text including the next newline character. Simply put, this operation will read the file line by line.

days_file.readline()

The result will be:

'Monday\n'

Once usedreadlineThe operation reads a line and the current reading position of the file will be moved to the next line. If this operation is called again, it will return the next line in the file as follows:

days_file.readline()

'Tuesday\n'

Use readlines

The last operationreadlines()Returns a list of each line in the file, where each item in the list represents one line.

days_file.readlines()

['Monday\n', 'Tuesday\n', 'Wednesday\n', 'Thursday\n', 'Friday\n', 'Saturday\n', 'Sunday\n']

One thing to remember when reading a file is that once one of the read operations is used, the file cannot be read again. For example, if you run days_file.read() first, and then days_file.readlines(), the second operation will return an empty string. So whenever you want to read from a file, you must first open a new file variable or use the seek() method, which is beyond the scope of this tutorial. If you want to learn more, Python has good documentation on these methods.

Now that we have read the content from the file, let's learn how to write to a new file.

Step 4 ——Write to the file

In this step, you will write a new file that includes the title Days of the Week, followed by the contents of the first file. First, create atitleVariable.

title = 'Days of the Week\n'

You also need to store the days of the week in a variable, which we calldays. This code opens the file in read mode, reads the file, and stores the output returned by the read operation in our new variabledaysmiddle. For easier understanding,Step 2The code in  is already included.

path = '/home/sammy/'
days_file = open(path, 'r')
days = days_file.read()

Now that you have variables for the title and days of the week, you can start writing to the new file. First, specify the location of the file. Similarly, we will use the directory/home/sammy/, so our path will be/home/sammy/new_days.txt. Then, you can useopen()The function opens a new file in write mode, specify'w'model.

new_path = '/home/sammy/new_days.txt'
new_days = open(new_path, 'w')

Once a new file is opened, you can use the write() method to add data. This method takes a string parameter and writes the data to a file. If you want to start a new line in the file, you must explicitly provide the newline character \n, which is already included when you assign 'Days of the Week\n' to the title variable.

Write the title to the file, then the days of the week. It may help to add some print statements to track what we want to write to the file, which is usually used to track the progress of the script.

new_days.write(title)
print(title)

new_days.write(days)
print(days)

Finally, it is important to close the file when you complete the operation of the file.

Step 5 ——Close the file

Close the file to ensure that the connection between the file on the disk and the file handle has ended. Close files also ensures that other programs can access them and keep your data secure. If you don't have a likeStep 6Use as described inwithStatement, be sure to close your file. In this example, useclose()Method closes all files.

days_file.close()
new_days.close()

Now the script completes the operation on the file, it usesclose()The method releases the file handle.

Step 6 ——Use the with statement (optional)

The recommended way to process files in Python (usually called the Pythonic way) is a feature of using the language called the with statement. These statements are abbreviated to set a context to work, and once the context is finished, the final details will be processed automatically to prevent common errors. In case of processing files, the with statement will automatically close the file so that once you complete the task, there will be no file handle remaining.

Like any block in Python, such as a function definition, an if statement, or a loop, the with statement takes a simple statement followed by a :, followed by an indented code block. Here is a code example that opens a file and prints its contents:

with open('/home/sammy/', 'r') as days_file:
    days = days_file.read()
    print(days)

Let's understand the role of this code step by step. As before, we use Python's built-in open() function to open the file, passing the file path and pattern parameters. However, since we used the with statement instead of assigning the result's file handle to a variable with =, we used the as keyword to assign. This is part of the full with statement syntax:

with action as result:
    . . .

After : we move to the next line and indent our code, which is how Python organizes function blocks. As before, we can access the file handle in the days_file variable, so we can call the read() method to get everything and print() out.

Note that the close() method is not called here. This is because once the code leaves this block (that is, once the next line of code is no longer indented, or the file is over), the context of the with statement knows that the files are automatically closed. The benefit of this is not only that you don't need to remember to close the file every time, but that all the logic you're working with a particular file is visually and logically clear within that block. This structure helps keep the code clear and readable.

With this understanding, let's rewrite our previous code using the with statement:

with open(path, 'r') as days_file, open(new_path, 'w') as new_days:
    days = days_file.read()
    
    new_days.write(title)
    new_days.write(days)
    
print(title)
print(days)

The code is now more organized. We first defined some variables as before: the path to our two files and the title we will use for the new file title. After that, we start our with statement, open our two files, and store their connections in appropriately named variables. As before, we read the contents of days_file and then write the title and these to new_days. Finally, we end the block by unindenting the code block to print the values ​​of title and days we read.

While there are some cases where you would want to use the close() method described earlier, and it is important to understand how it works, you usually use the with statement when working with files in Python.

Step 7 — Check our code

Before running the code, it's best to make sure everything looks right. The final product should be like this:

path = '/home/sammy/'
new_path = '/home/sammy/new_days.txt'
title = 'Days of the week\n'

with open(path, 'r') as days_file, open(new_path, 'w') as new_days:
    days = days_file.read()
    
    new_days.write(title)
    new_days.write(days)
    
print(title)
print(days)

After saving the code, open the terminal and run your Python script like this:

python 

The output will look like this:

Days of the Week

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

Now, by opening a new file (new_days.txt) to completely check if the code is running properly. If everything goes well, it should include the following:

Days of the Week
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

in conclusion

In this tutorial, we learned how to process and manipulate plain text files in Python 3. Now you can open, read, write, and close files in Python, and you can continue to process your own data in Python. Python provides many other useful methods when processing input and output, and also provides documentation for further learning.

The above is the detailed tutorial on processing and manipulating plain text files in Python3. For more information about processing and manipulating text files in Python3, please pay attention to my other related articles!