object oriented programming
Python
Python offers a wide variety of structures that allow you to save information such as Dictionaries or Lists, however, if the application is closed, that information is lost. To prevent this from happening, we can make use of files to save the information so that we can access it again. Next, we will see a short example of how to read the information from a txt
file.
1Hello, this is an example file. 2We are learning how to read files in Python. 3 4It's exciting!
1with open("test.txt", "r") as file: 2 3 4 print("Read all lines in a file:\n") 5 print(file.read())
code output:
1Read all lines in a file: 2 3 4Hi, this is a sample file. 5We are learning to read files in Python. 6 7¡It’s exciting!
In this example, we want to read the information stored in file test.txt
, to do this, we can use the Python open()
method, this method receives two parameters, the first one is the name of the file we want to open and the second one is the way we want to open it, in our example "r"
(read). Then we use the method read()
to print the information on the console.
As said before, to read the content of a file we need the Python open()
method, this method receives two parameters, the first one is the name of the file we want to open, and the second one is the way we want to open the file. The second parameter can receive different values; in the following table you can see the most relevant ones with a small description of each:
Value | Description |
---|---|
"r" | Opens the file in read mode. You can read the contents of the file, but you cannot modify it or add content to it. |
"r+" | Opens the file in read/write mode. You can read and modify the contents of the file. |
"w" | Opens the file in write mode. If the file already exists, its content will be deleted and new content can be written, and if the file does not exist, a new one will be created. |
"w+" | Is similar to "r+" but unlike this one, it creates a new file if it does not exist and if the file already exists and has information, it removes all the information from the file and adds the new one. |
"a" | Opens the file in attachment mode. If the file already exists, you will be able to add content to the end of the existing information. If it does not exist, a new one will be created. |
"a+" | Opens the file in attach and read mode. You can read and add content at the end of the file. |
"x" | Opens the file in create mode. Creates a new file for writing, but if the file already exists, it will throw an error. |
"x+" | Opens a file in create and read/write mode. Similar to "w+" , but throws an error if the file already exists. |
"b" | Binary file mode, used in conjunction with other modes such as "rb" (read binary) "wb" (write binary). |
The following is an example of how to read a txt
file and some of the most commonly used methods.
Note, in these examples, we will use the syntax
with open(file_name, read_mode) variable_name
since this syntax takes care of closing the file automatically once we have finished working with it.
1This is a test text. 2We will learn how to use the readline() method to read only one line of a file, 3 4and we will also learn how to use the read() method to read all the lines of the file.
1with open("test.txt", "r", encoding="utf-8") as file: 2 print("File in line 1: ", file.readline()) 3 print("File in line 2: ", file.readline()) 4 5 print("--------------------------------------") 6 print("All the lines of the file:") 7 file.seek(0) 8 print(file.read()) 9 10 print("\n--------------------------------------") 11 print("Extract all the lines of the file in a list:") 12 file.seek(0) 13 data = file.readlines() 14 print(data)
Code output:
1File in line 1: This is a test text. 2File in line 2: We will learn how to use the readline() method to read only one line of a file, 3 4-------------------------------------- 5All the lines of the file: 6This is a test text. 7We will learn to use the readline() method to read only one line of a file, 8 9and we will also learn how to use the read() method to read all the lines of the file. 10 11-------------------------------------- 12Extract all lines of the file in a list: 13[ 14 'This is a test text.\n', 15 'We will learn how to use the readline() method to read only one line of a file,\n', 16 '\n', 17 'and we will also learn how to use the read() method to read all the lines of the file.' 18] 19
The contents of a file can be read with several Python methods, in this example we see the three most relevant methods, the method readline()
reads only one line of the file, if you call it three times in a row then you will have the first three lines of the file, you can also pass an integer as a parameter that tells it how many characters to read, for example if you pass 4 as parameter readline(4)
will return the text "This" because those are the first 4 characters of the first line. The method read()
shows all the lines of the file and the method readlines()
returns a list with all the lines in the file.
To modify the contents of a file, the Python open()
method offers several alternatives, here are some examples of how to modify the contents of a file txt
.
Assuming the following text file, test.txt
1Earth, our blue planet, is a haven of life in the vast cosmos. Oceans and continents intertwine in a dance of biodiversity. Mountains rise while valleys spread out, creating diverse landscapes.
We will see two of the most relevant examples of how you can modify this file. For the first example, we will use the open()
method and we will pass as a second parameter the value of "w"
(write) which allows you to modify the original file but removes the previous values. For the second example, we will use the value of "a"
(append) which allows you to modify the file without the need to delete its previous content.
1with open("test.txt", "w", encoding="utf-8") as file: 2 new_line = "\nThis is a new line of text added to the test.txt file" 3 file.write(new_line)
With the execution of the above code, the file test.txt
would look like this:
1This is a new line of text added to the test.txt file
To modify the contents of a file we can use the method write()
of Python, this method receives as parameter the text with which you want to modify the file, in this example, we make use of open()
method and pass as a second parameter the value of "w"
, this value allows you to modify the content of a file, but before adding the new content, it removes the one that the file previously had.
1with open("test.txt", "a", encoding="utf-8") as file: 2 new_line = "\n \nThis is a new line of text added to the test.txt file." 3 file.write(new_line)
When executing the above code, the text.txt
would look like this:
1Earth, our blue planet, is a haven of life in the vast cosmos. Oceans and continents intertwine in a dance of biodiversity. Mountains rise while valleys spread out, creating diverse landscapes. 2 3This is a new line of text added to the test.txt file
In this example, we again make use of the Python open()
method but this time we pass as a second parameter the value of "a"
which allows you to modify the contents of a file, but unlike the "w"
value, this adds the new content to the end of the existing content. These two values are very important and you can use them depending on the task you need to perform in the file, remember that adding the symbol "+"
at the end of each of these values "w+"
, "a+"
you will be able to read and write to a file.
txt
files can be used to store information such as a list of dictionaries with any type of information. Here is an example of how to use a txt
file to store the information of three test users.
To be able to enter a list of dictionaries in a txt
file first we need to convert it to a json
(JavaScript Object Notation) datatype. For this we will use the json
module (Check out this article to remember how to use the modules in Python.
1import json 2 3data = [ 4 {"name": "Thomas", "lastname": "Smith", "age": 35}, 5 {"name": "Jonathan", "lastname": "Smith", "age": 53}, 6 {"name": "Jane", "lastname": "Watson", "age": 29} 7] 8 9with open("test.txt", "w+", encoding="utf-8") as file: 10 json.dump(data, file, indent=4)
When executing this code, the file test.txt
should look as follows:
1[ 2 { 3 "name": "Thomas", 4 "lastname": "Smith", 5 "age": 35 6 }, 7 { 8 "name": "Jonathan", 9 "lastname": "Smith", 10 "age": 53 11 }, 12 { 13 "name": "Jane", 14 "lastname": "Watson", 15 "age": 29 16 } 17]
In this example, we use the json
module and the dump()
method to convert the dictionary list into a file of type json
and save it to the test.txt
file, the dump()
method receives three parameters, the first parameter is the information that you want to save in the txt
file, this parameter is mandatory. The second parameter is the variable file
which tells the method where to save the information, this parameter is also mandatory. The third parameter is the variable indent=4
which tells the method how to format the information inside the txt
file, this parameter is optional.
To read the txt
file and convert the information back into a list, we also need to use the json
module, this module has a method called load()
which converts information of type json
back to a list of dictionaries, as shown in the following example:
1import json 2 3data = [ 4 {"name": "Thomas", "lastname": "Smith", "age": 35}, 5 {"name": "Jonathan", "lastname": "Smith", "age": 53}, 6 {"name": "Jane", "lastname": "Watson", "age": 29} 7] 8 9with open("test.txt", "w+", encoding="utf-8") as file: 10 # The dump() method enters the information into the txt file. 11 json.dump(data, file, indent=4) 12 13 # The load() method reads the information from the txt file and converts it back into a list of dictionaries. 14 15 file.seek(0) 16 recovered_data = json.load(file) 17 18 print("Users information: ", recovered_data)
Code output:
1Users information: [ 2 {'name': 'Thomas', 'lastname': 'Smith', 'age': 35}, 3 {'name': 'Jonathan', 'lastname': 'Smith', 'age': 53}, 4 {'name': 'Jane', 'lastname': 'Watson', 'age': 29} 5]
The load()
method receives as parameter the variable file
that tells which file to read and convert back into Python code, this method returns the user information that is stored in the variable recovered_data
, finally we simply print the results in the console.
File manipulation is an essential skill in programming, in Python we can manipulate any type of file whether it is a txt
file, a json
type file, etc... In this article we have explored how to read and modify files with the help of the open()
function of Python. We also saw how to add and modify more complex data structures such as a list of dictionaries.
I hope you found this article useful and that it has helped you to better understand how to manipulate files with the help of Python methods,
remember to practice as much as you can and create your own applications to reinforce the knowledge learned in this article.
¡Enjoy learning how to manipulate files with the help of Python! 😉👋