Read, Write & Append Files in Python
It's time ti learn how to read, write and append data to different files using Python. We're going to focus on Text, CSV and JSON file formats in this lesson. (I'll also cover Excel files in another lesson).
So let's begin with the basics. Ready?
Basics of Working with Files in Python
Let's start with Text files and the basics.
To read the contents of any file, you use Python's built-in open() function. You provide a file name and a mode ('r', 'w', 'a'...).
Here's simple example:
filename = 'example.txt'
file = open(filename, 'r')
content = file.read()
print(content)
file.close()
filename = 'example.txt'
file = open(filename, 'r')
content = file.read()
print(content)
file.close()
filename = 'example.txt'
file = open(filename, 'r')
content = file.read()
print(content)
file.close()
This workflow works, but it's not recommended way because you have to manually close files. If you forget or get an error - there's a change to corrupt files and lose your data. Not good.
Better Way To Read/Write Files
To avoid issues with closing files, we can use a special context-manager for open function, which will automatically close your files so you don't have to worry about it. Even if you get an error while reading.
Here's how to use it:
filename = 'example.txt'
with open(filename, 'r') as file:
content = file.read()
print(content)
filename = 'example.txt'
with open(filename, 'r') as file:
content = file.read()
print(content)
filename = 'example.txt'
with open(filename, 'r') as file:
content = file.read()
print(content)
It's pretty much the same workflow but simplified!
Reading line by line (with numbering)
Let's push it a step further and read contents line by line and write the line number.
filename = 'Test/example.txt'
filepath = r'C:\Users\EF\Documents\EF Documents\example.txt'
with open(filename, 'r') as file:
content = file.read()
for n, line in enumerate(content.splitlines()):
print(n, line)
filename = 'Test/example.txt'
filepath = r'C:\Users\EF\Documents\EF Documents\example.txt'
with open(filename, 'r') as file:
content = file.read()
for n, line in enumerate(content.splitlines()):
print(n, line)
filename = 'Test/example.txt'
filepath = r'C:\Users\EF\Documents\EF Documents\example.txt'
with open(filename, 'r') as file:
content = file.read()
for n, line in enumerate(content.splitlines()):
print(n, line)
This way you'll read your file line by line and print their line numbers using enumerate function.
💡enumerate - will create a pairing index numbers for each item in a container. Great for using in loops for keeping track of your iterations.
✍️How To Write/Append Data to a File?
Alright, you know how to read, what about Writing or Appending data?
Well, first of all we need to add another mode in arguments ('w' or 'a'). Write mode will create a blank file so you'll override existing files, while Append mode will add data to the end of the file.
Also keep in mind, if you try to read while in write or append mode, you'll get an error message io.UnsupportedOperation: not readable
----------------------------------------------------------------------------------------------------
filename = 'example.txt'
with open(filename, 'a') as f:
f.write('Hello World!\n')
f.write('Hello World!\n')
f.write('Hello World!\n')
filename = 'example.txt'
with open(filename, 'w') as f:
f.write('Hello!\n')
f.write('Hello!\n')
f.write('Hello!\n')
f.write('Hello!\n')
f.write('Hello!\n')
----------------------------------------------------------------------------------------------------
filename = 'example.txt'
with open(filename, 'a') as f:
f.write('Hello World!\n')
f.write('Hello World!\n')
f.write('Hello World!\n')
filename = 'example.txt'
with open(filename, 'w') as f:
f.write('Hello!\n')
f.write('Hello!\n')
f.write('Hello!\n')
f.write('Hello!\n')
f.write('Hello!\n')
----------------------------------------------------------------------------------------------------
filename = 'example.txt'
with open(filename, 'a') as f:
f.write('Hello World!\n')
f.write('Hello World!\n')
f.write('Hello World!\n')
filename = 'example.txt'
with open(filename, 'w') as f:
f.write('Hello!\n')
f.write('Hello!\n')
f.write('Hello!\n')
f.write('Hello!\n')
f.write('Hello!\n')
💡Don't forget to use \n to create new lines in your files. Otherwise you'll keep writing everything into one line.
Quick Example
Let's create a quick exampel to write data to a new file, and if it already exists we'll just read its contents.
import os
filename = 'example2.txt'
if not os.path.exists(filename):
print(f'Creating a New File: {filename}')
with open(filename, 'w') as f:
f.write('Hello!\n')
f.write('Hello!\n')
f.write('Hello!\n')
f.write('Hello!\n')
f.write('Hello!\n')
else:
print('File Exists!')
print('Content:')
with open(filename, 'r') as f:
print(f.read())
import os
filename = 'example2.txt'
if not os.path.exists(filename):
print(f'Creating a New File: {filename}')
with open(filename, 'w') as f:
f.write('Hello!\n')
f.write('Hello!\n')
f.write('Hello!\n')
f.write('Hello!\n')
f.write('Hello!\n')
else:
print('File Exists!')
print('Content:')
with open(filename, 'r') as f:
print(f.read())
import os
filename = 'example2.txt'
if not os.path.exists(filename):
print(f'Creating a New File: {filename}')
with open(filename, 'w') as f:
f.write('Hello!\n')
f.write('Hello!\n')
f.write('Hello!\n')
f.write('Hello!\n')
f.write('Hello!\n')
else:
print('File Exists!')
print('Content:')
with open(filename, 'r') as f:
print(f.read())
Sign-Up For Future Updates✨
Be among the first people to hear about
New Python Courses or Useful Resources!
Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!
Sign-Up For Future Updates✨
Be among the first people to hear about
New Python Courses or Useful Resources!
Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!
Sign-Up For Future Updates✨
Be among the first people to hear about
New Python Courses or Useful Resources!
Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!
Extra Open() Modes
Before we keep going to the next file types I want you to pay your attention to the following table. You'll notice that you can also use r+, w+ and a+ modes. There are more, but these are the most common.
Using special modes can help you achieve more functionality like reading and writing data using the same mode. Here's the overview:
I forgot to mention this part in the tutorial but you can give it a try and experiment.
Moving To CSV Files
Next, let's talk about CSV file types and why it's different.
CSV - is a lightweight spreadsheet format for keeping your data as rows and columns separated by a delimiter symbol (comma / tab...). It's useful because it can be imported in Excel and similar software with the right structure.
Example:
Now to read/write, we could use the same workflow and treat the data as plain text. It will work, but that's not convenient and there is a better way to do that.
We'll use Python's built-in csv module. The idea is that we'll read the plain text from a file and then we'll convert it into more usable data-structure in Python to keep meaning of rows and columns.
Now let's cover Examples.
Read CSV:
You'll notice that we start reading the file with the same open() function, but then we use csv.reader(f) function so we can iterate over data with loops and read rows and items separately.
import csv
filename = 'items.csv'
with open(filename, 'r') as f:
reader = csv.reader(f)
for row in reader:
print(row)
import csv
filename = 'items.csv'
with open(filename, 'r') as f:
reader = csv.reader(f)
for row in reader:
print(row)
import csv
filename = 'items.csv'
with open(filename, 'r') as f:
reader = csv.reader(f)
for row in reader:
print(row)
Append / Write CSV
When writing CSV files on most platforms, pass newline='' to avoid blank lines between rows.
import csv
filename = 'items.csv'
with open(filename, 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow(['Toilet', 'Gold', '99999'])
filename = 'items.csv'
with open(filename, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['Toilet', 'Gold', '99999'])
import csv
filename = 'items.csv'
with open(filename, 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow(['Toilet', 'Gold', '99999'])
filename = 'items.csv'
with open(filename, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['Toilet', 'Gold', '99999'])
import csv
filename = 'items.csv'
with open(filename, 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow(['Toilet', 'Gold', '99999'])
filename = 'items.csv'
with open(filename, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['Toilet', 'Gold', '99999'])
Choose append if you want to keep adding rows; choose write to start fresh.
JSON Files
Now we can talk about JSON file format (got it?).
JSON is essential for dictionary like structures. It's a supper common format but not always very human friendly to read when it's raw. But there are nice viewers online to help you with that.
Similar to previous example, it also has a built-in module - json that will help us read the data the right way. Afterall, getting JSON data in a plain text is like a suicide mission.
Read JSON
import json
with open('materials.json', 'r') as f:
data = json.load(f)
mats = data['materials']
for mat in mats:
print(mat['name'])
print(mat['density'])
import json
with open('materials.json', 'r') as f:
data = json.load(f)
mats = data['materials']
for mat in mats:
print(mat['name'])
print(mat['density'])
import json
with open('materials.json', 'r') as f:
data = json.load(f)
mats = data['materials']
for mat in mats:
print(mat['name'])
print(mat['density'])
Write Data to JSON
Now let's look at how to write data to JSON.
Keep in mind that there is no such thing as append to JSON. There are too many possibilities where to add data... However, we can read the data as a dict, then modify it however we like, and then write it to a new file or override existing.
This would be an alternative to appending data, so let's look at it as an example
import json
with open('materials.json', 'r') as f:
data = json.load(f)
new_material = {"id": "M005",
"name": "Plaster",
"density": 250,
"cost_per_1000_units": 100
}
data['materials'].append(new_material)
with open('materials.json', 'w') as f:
json.dump(data, f, indent=4)
import json
with open('materials.json', 'r') as f:
data = json.load(f)
new_material = {"id": "M005",
"name": "Plaster",
"density": 250,
"cost_per_1000_units": 100
}
data['materials'].append(new_material)
with open('materials.json', 'w') as f:
json.dump(data, f, indent=4)
import json
with open('materials.json', 'r') as f:
data = json.load(f)
new_material = {"id": "M005",
"name": "Plaster",
"density": 250,
"cost_per_1000_units": 100
}
data['materials'].append(new_material)
with open('materials.json', 'w') as f:
json.dump(data, f, indent=4)
💡If you don't pass indent, you'll get a one-liner JSON file. Avoid it by providing indent=4.
Wrap-Up
And that's the basics you need to know for working wit h Text, CSV and JSON file formats.
For now understand how to use open via context-manager and remember that there are csv and json modules available to make your life easier.
Happy Coding!
🙋♂️ See you in the next lesson.
- EF
Happy Coding!
🙋♂️ See you in the next lesson.
- EF
Happy Coding!
🙋♂️ See you in the next lesson.
- EF