How to sort the lines of a text file in Python:
This post will show you how to sort the lines of a text file in Python. We will learn how to sort the lines in ascending order. It will sort the lines alphabetically or similar to a dictionary.
We can also create another file to write the sorted lines to it. We will open the file in read mode, sort the lines of the file and write the sorted lines to a different file.
With this program, you will learn how to do the following file operations in Python:
- Open a file in read or write mode.
- How to read the content of a file.
- How to write content to a file.
- How to sort an array of items.
How to open a file in Python:
We need to open a file if we want to read or write data to it. There is one method called open() to open a file. It returns a file object. We can use that file object to perform file operations.
This method is defined as like below:
Here, file is the path of the file. mode is used to define the mode for opening the file. This is an optional value. It can be:
- ‘r’ or read mode. This is the default option. This mode is used to read the content of a file. If the file doesn’t exist, open will throw an error with this mode.
- ‘w’ or write mode. This mode is used to write content to a file. If the file doesn’t exist, it will create a new file.
- ‘a’ or append mode. This mode is used to append content to a file. Similar to write mode it also creates a new file if the file doesn’t exist.
- ‘x’ is to open a file for exclusive creation. It will create the file if it doesn’t exist, else it throws an error.
- ‘t’ is used to open the file in text mode. This is the default option.
- ‘b’ is used to open the file in binary mode.
- ‘+’ is used to open for updating the file.
For this example, we will open the given file in read mode. It will return the file object, that can be used to read the content of the file. Once the reading and sorting is done, we will open the second output file in write mode and write the sorted lines.
This program will use the below algorithm:
- Open the first file in read mode.
- Declare an empty array to hold the content of the file.
- Read the lines of the file and add it to the array. Close the file once the reading is done.
- Sort the content of the array.
- Open the output file in write mode.
- Write the sorted content to the output file. Once done, close the file.
Below is the complete python program:
def sort_file_content(in_path, out_path): lines =  with open(in_path) as in_f: for line in in_f: lines.append(line) lines.sort() with open(out_path, 'w') as out_f: for line in lines: out_f.writelines(line) if __name__ == "__main__": input_file = "input.txt" output_file = "output.txt" sort_file_content(input_file, output_file)
- sort_file_content method is used to sort the content of a file and write it to another file. It takes the path of two files as the parameters. The first one is the input file path and the second one is the output file path.
- lines is an empty array to hold the content of the input file.
- We are using with blocks to open the files. We don’t have to close the files if we use while.
- The first while is used to open the file to read its content. It reads the content of the file or lines of the file and appends all to the array lines. We don’t have to pass the mode as we are opening it to read the content. By default it takes read mode.
- The sort() method is used to sort the content of the array lines.
- The second while is used to open the second file in write mode. ‘w’ is passed to the open method. Inside this block, we are using a for loop to iterate over the contents of the array and writing all to the file.
For example, if the input.txt file has the below content:
that is a cat hello world hello arnold how are you hello universe where are you
It will write the below content to the output.txt file:
hello arnold hello universe hello world how are you that is a cat where are you
You might also like:
- Python dictionary fromkeys method
- Python dictionary pop method explanation with examples
- 11 differences between Python 2 vs Python 3 with examples
- Learn Python numpy clip method with examples
- How to find Determinant in Python Numpy
- Get evenly spaced numbers in an interval using numpy linspace
- Python string isnumeric method
- How to use Python string partition method
- Python numpy floor_divide method explanation with examples
- How to use logical operator with strings in Python
- Python Matplotlib horizontal histogram or bar graph