Python Read Write Files
Python has various methods for performing operations against files. Here's an overview.
Open a File
When you read or write a file, the first thing you need to do is open it (or create it). Python provides the
open() method that is used to open a file. It also creates the file if it doesn't already exist.
open() syntax is like this:
It takes two parameters. The first one provides the name of the file, and the second parameter specifies the mode to be used. A mode of
r means "read", mode of
w means "write", and
a means "append". You can append the mode with
b to specify binary mode. You can also do stuff like
r+ to make it read and write.
Create a File
Here's an example of creating a new file:
The first line creates the file (because it doesn't already exist) and opens it in write mode (because we specified
w). If the file already existed, it would simply open the existing file in write mode. We assign the file to
f so that we can perform operations against it.
We then write some text to the file using the
Once we're done, we close the file. This is good practice, as it will free up system resources.
Read a File
Now that we've created a file and added some content to it, we can read it. Here's how:
It's very similar to the previous example, in that we open the file, do our stuff, then close it.
The main differences are:
- This time we use
ras the mode
- We use
read()to read the file (and assign it to a variable we call
- We use
print()to print the contents of the file
Notice that, because we assigned the contents to a variable, we could print the file's contents after we closed the file. Basically, we opened the file, copied what we needed, then closed the file again.
Update a File
write() method can also be used to update the file. To do this, just open the file and apply
write() with the updated contents. Note that this will completely replace the existing content with the new content.
Original content: Hello World! Updated content: Hey there Jupiter!
Append a File
You can also append content to a file. This means that you can add content to the existing content. In other words, the original content remains intact, and your new content is appended to the end.
To do this, simply change the
w argument to
a when updating the file using the
open() method. This opens the file in "append" mode.
Original content: Hey there Jupiter! Updated content: Hey there Jupiter! Hey there Uranus!
Read a Portion of the Content
By default, the
read() method returns all content in the file. If you only want a portion of the content, you can provide a number as an argument. This will specify how many characters are returned. Like this:
Read a Single Line
You can use
readline() to read a single line in the file:
Line 1: Hey there Jupiter! Line 2: Hey there Uranus!
Looping over the Lines
You can use a
for loop to loop over each line in the file. Here's how that works:
Line: Hey there Jupiter! Line: Hey there Uranus!
Read Multiple Lines as a List
You can use the
readlines() or the
list() methods to return all lines in a list. Like this:
['Hey there Jupiter!\n', 'Hey there Uranus!'] ['Hey there Jupiter!\n', 'Hey there Uranus!']
As you can see by the format, they both return the contents as a list, with each line as a separate list item.