This tutorial was written using Python 3.6
Being able to effectively work with the filesystem in any programming language is important as there will always be a need to do things such as import data from files or store data in local files. In this tutorial we’ll be looking at how you can create files, write to these newly created and then subsequently delete these files once we are done.
There are multiple ways to create files in Python, but arguably the cleanest way to do this is by using the
with keyword like so:
Context Manager Approach
What this does is open a file named
test.txt in a context manager and returns this new file object to us as
file. Within the bounds of this context manager we then call
file.write("THIS IS A TEST FILE") which subsequently writes
THIS IS A TEST FILE to our newly created file. Upon exiting our
with statement the program then handles closing the file for us and we don’t have to worry about anything else.
Without a Context Manager
However should you wish to do this in a manner that doesn’t use a context manager then you can do something like so:
The above code creates a new file called
test2.txt, it then appends
This is a second test to that file before closing the file by calling
file.close(). It’s important to note that by going down this route we have to handle the closing of these files ourself. Forgetting to do this can lead to complications such as:
- It can impact the performance of your program.
- Some of the changes you make to files in Python may not actually be written to the file until after the call to
- If you are dealing with thousands of files then you may hit limits as to how many files can be open on your machine.
More importantly, it’s considered bad practice.
Writing to a File
In the previous two examples you’ll notice that we called the
open() function with
'w' as our second parameter.
When it comes to working with files in Python, it’s important to bear in mind that there are different
modes that you can open a file with. This allows you to have more fine-grained control when you are working with files.
Ensuring you have used the correct mode when opening files can potentially save you quite a bit of debugging time. In the past I have found myself scratching my head wondering why the
w mode is overwriting all of my file’s content when I should indeed have used the
Reading a File Line-by-Line
It’s not an uncommon use-case to want to iterate over a file in a line-by-line fashion and process each line as it comes in. When you open a file you can actually use it as an iterator and iterate over it in the same manner that you would with normal iterators in Python.
Let’s take for example a text file called
test3.txt that contains the following:
If we wished to iterate over this in a line-by-line fashion we could do something like so:
This would then print out the following when we ran this program: