= "11.txt.utf-8"
file_name file = open(file_name, 'rt')
Read and write files
Resources
- Real Python working and reading and writing files tutorials.
- Reading and Writting files section in the official Python tutorial.
- Context managers in Real Python.
- Official documentation for the with statement.
- A Real Python tutorial about flush.
- If you ever need to work with temporary files, the tempfile module is very useful.
open
In many ocassions we will need that our programs read information stored in files, thus a very common action will be to open a file. In Python we could open a file with the open function.
Let’s imagine that we have a text file stored in the same directory/folder as our Python code. For instance, we could had download Alice in wonderland from Project Gutenberg. This file is named 11.txt.utf-8. Once download it, we could open it using the following code.
The open function can take several parameters, but the most important ones are the file path and the mode.
The mode indicates if we are trying to open an existing file for reading (r mode) or creating a new file for writing (w mode). Moreover, it indicates if we are dealing with a text or [binary] (https://en.wikipedia.org/wiki/Binary_file) f*ile. In this example the mode was “rt”, so we are trying to open an existing text file for reading.
In this case, the open function returns to us a Text IO file object that allows us to read the file.
Read text
Now that we know how to open a file, let’s try to read it. The most common way to read a text file is to use a for loop.
= "11.txt.utf-8"
file_name file = open(file_name, 'rt')
for line in file:
print(line)
Try to print only the first 10 lines.
Write
We could also create a new file to write in it by using the mode “wt” (write, text). Once we have the file object we can write in it using the write method.
= "my_output_file.txt"
file_name file = open(file_name, 'wt')
file.write("Hello world")
Create a file, and write in it two lines, like “Hi” and “Bye”, in it.
close and with
So far we have not closed our file objects after we are done with them, but we really should. So, the proper way to read a file would be:
= "some_file.txt"
file_name file = open(file_name, 'rt')
for line in file:
print(line)
file.close()
The number of file objects that we can open are limited by the operating system, so that’s one reason for closing them. If we don’t do it, Python will try to close them for us after the variable that holds the file object goes out of scope, but it’s better if we do it by ourselves.
Even better, we could use the with statement. with will take care of closing the file after finishing the with block, even in the event of an exception ocurring within the block.
= "some_file.txt"
file_name
with open(file_name, 'rt') as file:
for line in file:
print(line)
Buffers and flush
Closing the file is even more important when we are writing in them than we are reading from them. We could think that every time that we use the write method Python writes the given contents in disk, but this is not so. In fact what the write method does is to write the given content in a memory buffer. It writes in memory because memory is much faster than disk, and thus iIt would be very slow to write down in disk every time. When does Python decides to really write down in disk? Well, from time to time, but usually we do not take care of that. What we know for sure is that Python writes down everything that is to write when whe close the file calling the close method. So, close the file.
If you need to make sure that Python writes down to disk everything you have written so far you could also use the flush method, but don’t do it every time that you write, because that would be very slow.