🚧Summaries For Lessons 24+ Are Still Work In Progress!

“Whoever is patient has great understanding…" Proverbs 14:29

Files I/O: How to Read/Write to txt,csv,json formats

Description here...

Files I/O: How to Read/Write to txt,csv,json formats

Description here...

Files I/O: How to Read/Write to txt,csv,json formats

Description here...

- Basics: How to Work with Files
- How to Read/Write/Append to TXT files
- How to Read/Write/Append to CSV files
- How to Read/Write/Append to JSON files

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:

#❌ PS: Not Recommended!
#----------------------------------------------------------------------------------------------------
filename = 'example.txt'
file     = open(filename, 'r')   # 1. Open
content  = file.read()           # 2. Read
print(content)
file.close()                     # 3. Close

#💡PS It's important to close files!
#❌ PS: Not Recommended!
#----------------------------------------------------------------------------------------------------
filename = 'example.txt'
file     = open(filename, 'r')   # 1. Open
content  = file.read()           # 2. Read
print(content)
file.close()                     # 3. Close

#💡PS It's important to close files!
#❌ PS: Not Recommended!
#----------------------------------------------------------------------------------------------------
filename = 'example.txt'
file     = open(filename, 'r')   # 1. Open
content  = file.read()           # 2. Read
print(content)
file.close()                     # 3. Close

#💡PS It's important to close files!

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:

#1️⃣ Read Text Files
#----------------------------------------------------------------------------------------------------
filename = 'example.txt'

with open(filename, 'r') as file:
    content = file.read()
    print(content)
#1️⃣ Read Text Files
#----------------------------------------------------------------------------------------------------
filename = 'example.txt'

with open(filename, 'r') as file:
    content = file.read()
    print(content)
#1️⃣ Read Text Files
#----------------------------------------------------------------------------------------------------
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.

#1️⃣ Read Text Files
#----------------------------------------------------------------------------------------------------
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)
#1️⃣ Read Text Files
#----------------------------------------------------------------------------------------------------
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)
#1️⃣ Read Text Files
#----------------------------------------------------------------------------------------------------
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

#2️⃣ Append Text
----------------------------------------------------------------------------------------------------
filename = 'example.txt'
with open(filename, 'a') as f:
    # content = f.read() #io.UnsupportedOperation: not readable

    f.write('Hello World!\n')
    f.write('Hello World!\n')
    f.write('Hello World!\n')

#3️⃣ Write Files
#----------------------------------------------------------------------------------------------------
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')
#2️⃣ Append Text
----------------------------------------------------------------------------------------------------
filename = 'example.txt'
with open(filename, 'a') as f:
    # content = f.read() #io.UnsupportedOperation: not readable

    f.write('Hello World!\n')
    f.write('Hello World!\n')
    f.write('Hello World!\n')

#3️⃣ Write Files
#----------------------------------------------------------------------------------------------------
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')
#2️⃣ Append Text
----------------------------------------------------------------------------------------------------
filename = 'example.txt'
with open(filename, 'a') as f:
    # content = f.read() #io.UnsupportedOperation: not readable

    f.write('Hello World!\n')
    f.write('Hello World!\n')
    f.write('Hello World!\n')

#3️⃣ Write Files
#----------------------------------------------------------------------------------------------------
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.

##️⃣ Example
#----------------------------------------------------------------------------------------------------
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())
##️⃣ Example
#----------------------------------------------------------------------------------------------------
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())
##️⃣ Example
#----------------------------------------------------------------------------------------------------
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!


Want To Donate? Click here.

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!


Want To Donate? Click here.

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!


Want To Donate? Click here.

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

#1️⃣ Read CSV File
#----------------------------------------------------------------------------------------------------
filename = 'items.csv'
with open(filename, 'r') as f:
    # content = f.read()
    # print(content)

    reader = csv.reader(f)
    for row in reader:
        print(row)
        # for item in row:
        #     print(item)
import csv

#1️⃣ Read CSV File
#----------------------------------------------------------------------------------------------------
filename = 'items.csv'
with open(filename, 'r') as f:
    # content = f.read()
    # print(content)

    reader = csv.reader(f)
    for row in reader:
        print(row)
        # for item in row:
        #     print(item)
import csv

#1️⃣ Read CSV File
#----------------------------------------------------------------------------------------------------
filename = 'items.csv'
with open(filename, 'r') as f:
    # content = f.read()
    # print(content)

    reader = csv.reader(f)
    for row in reader:
        print(row)
        # for item in row:
        #     print(item)

Append / Write CSV

When writing CSV files on most platforms, pass newline='' to avoid blank lines between rows.

import csv

#2️⃣ Append/Write CSV File
#----------------------------------------------------------------------------------------------------
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

#2️⃣ Append/Write CSV File
#----------------------------------------------------------------------------------------------------
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

#2️⃣ Append/Write CSV File
#----------------------------------------------------------------------------------------------------
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

#1️⃣ Read JSON files
#----------------------------------------------------------------------------------------------------
with open('materials.json', 'r') as f:
    # content = f.read()
    # print(content)

    data = json.load(f)
    # print(data)
    # print(type(data))

    mats = data['materials']
    for mat in mats:
        print(mat['name'])
        print(mat['density'])
import json

#1️⃣ Read JSON files
#----------------------------------------------------------------------------------------------------
with open('materials.json', 'r') as f:
    # content = f.read()
    # print(content)

    data = json.load(f)
    # print(data)
    # print(type(data))

    mats = data['materials']
    for mat in mats:
        print(mat['name'])
        print(mat['density'])
import json

#1️⃣ Read JSON files
#----------------------------------------------------------------------------------------------------
with open('materials.json', 'r') as f:
    # content = f.read()
    # print(content)

    data = json.load(f)
    # print(data)
    # print(type(data))

    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

#2️⃣ Append to JSON files (Read + Write)
#----------------------------------------------------------------------------------------------------
# Read JSON
with open('materials.json', 'r') as f:
    data = json.load(f)

# New Material
new_material = {"id": "M005",
                "name": "Plaster",
                "density": 250,
                "cost_per_1000_units": 100
                }

# Append New Material
data['materials'].append(new_material)

# Override the File
with open('materials.json', 'w') as f:
    json.dump(data, f, indent=4)
import json

#2️⃣ Append to JSON files (Read + Write)
#----------------------------------------------------------------------------------------------------
# Read JSON
with open('materials.json', 'r') as f:
    data = json.load(f)

# New Material
new_material = {"id": "M005",
                "name": "Plaster",
                "density": 250,
                "cost_per_1000_units": 100
                }

# Append New Material
data['materials'].append(new_material)

# Override the File
with open('materials.json', 'w') as f:
    json.dump(data, f, indent=4)
import json

#2️⃣ Append to JSON files (Read + Write)
#----------------------------------------------------------------------------------------------------
# Read JSON
with open('materials.json', 'r') as f:
    data = json.load(f)

# New Material
new_material = {"id": "M005",
                "name": "Plaster",
                "density": 250,
                "cost_per_1000_units": 100
                }

# Append New Material
data['materials'].append(new_material)

# Override the File
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

Follow along the lesson and learn how to Read, Write and Append data to the most common file types - TXT, CSV, JSON. It's not so complicated and it can be really important for certain automations.

If something didn't make sense, copy the code, try it out and try to make your own changes. It's often that we learn by breaking someone's code and then reverse engineering it. Try it.

⌨️ Happy Coding!

⌨️ Happy Coding!

⌨️ Happy Coding!

If you have any questions, leave them in the YouTube Comments.

If you have any questions, leave them in the YouTube Comments.

If you have any questions, leave them in the YouTube Comments.

Have a Question?

Have a Question?

Have a Question?

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!


Want To Donate? Click here.

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!


Want To Donate? Click here.

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!


Want To Donate? Click here.

PS. Python can change your career and how you think about problems.
Be Careful 🙂

PS. Python can change your career and how you think about problems.
Be Careful 🙂

PS. Python can change your career and how you think about problems.
Be Careful 🙂

Sposored by LearnRevitAPI.com