Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.
Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.
For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.
It makes sense to start learning by reading and watching videos about fundamentals and how things work.
Data Science and Machine Learning - 16 wks
Full-Stack Software Developer - 16w
Search from all Lessons
Curated list of small interactive and incremental exercises you can take to get better at any coding skill.
Curated section of projects to build while learning with simple instructions, videos, solutions, and more.
Guides on different topics related to the technologies that we teach in our courses
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
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:
|Opens the file in read mode. You can read the contents of the file, but you cannot modify it or add content to it.|
|Opens the file in read/write mode. You can read and modify the contents of the file.|
|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.|
|Is similar to |
|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.|
|Opens the file in attach and read mode. You can read and add content at the end of the file.|
|Opens the file in create mode. Creates a new file for writing, but if the file already exists, it will throw an error.|
|Opens a file in create and read/write mode. Similar to |
|Binary file mode, used in conjunction with other modes such as |
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_namesince this syntax takes care of closing the file automatically once we have finished working with it.
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
Assuming the following text file,
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.
With the execution of the above code, the file
test.txt would look like this:
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.
When executing the above code, the
text.txt would look like this:
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
"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 module (Check out this article to remember how to use the modules in Python.
When executing this code, the file
test.txt should look as follows:
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:
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! 😉👋