


Two cases arise after the execution of this statement. After this, we will open the specific file using statement given below.

But, we can also take the name of the file from the user as an input. Here, we take the file ‘sample.txt’ for simplicity. To sort the contents of a file, firstly we need to open the file in ‘read’ mode. Today, we will get to know how to open a file, a method to sort the contents of an opened file and a Python program that will create another file having the same content in sorted order. That is, we will sort the contents similar to the way in which words are listed in a dictionary. Here, we will sort the contents of a file in ascending order i.e. In this tutorial, we will learn a method for sorting the contents of a text file using a Python program.
