HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

3) File Input/Output Lesson

Python: Open File and Read CSV Data

6 min to complete · By Martin Breuss

After running your file counter script twice and saving the output to a CSV file, you'll end up with a file that might look something like this:

8,2,2,11
8,3,2,14

Your numbers will be different, but if you change the contents of the desktop, for example, by taking a few more screenshots before running the script again, you'll see that the numbers have changed.

Colorful illustration of a light bulb

Info: Your script only records very specific file types in your CSV file, namely the ones that you have explicitly added to the list that gets written to your CSV file. Adding new file types will be noticed by your file counter script, but with the current setup, it won't make it into your CSV file.

Every time you run your script, it'll add another line to this file.

Tasks

  • Improve your data by writing the headers to your CSV file during the first run of your script.
  • Think about how you could improve the CSV writer so that it records more file types. This is not a trivial task, and you'd have to approach the organization of your script differently. Map it out in your head and on paper to train thinking through designing a program.

So you've collected some data, and you can keep collecting it with your file counter script. Now, you'll also want to do something with the collected data! Time to start a new script and read in the data:

# analyze.py
import csv

with open("filecounts.csv", "r") as csvfile:
    reader = csv.DictReader(csvfile, fieldnames=["Folder", "CSV", "MD", "PNG"])
    counts = list(reader)

print(counts)

Some of this code is similar to when you were using the csv module to write the data to your file. However, you can see a few differences that you'll look at in more detail:

  • You're operating your file object in read mode ("r")
  • You're using the csv.DictReader() to read in the data.
  • Since you didn't add a header row to your CSV data, you need to define what each piece of data refers to by passing a sequence to the argument fieldnames. These values will become the keys for the dictionary that'll be created from each row of your data.
  • Finally, you need to convert the reader object to a list() in order to use it as expected.

After running the script, you should see output similar to below:

[
    {'Folder': '8', 'CSV': '2', 'MD': '2', 'PNG': '11'},
    {'Folder': '8', 'CSV': '3', 'MD': '2', 'PNG': '14'}
]

Your output might be formatted a bit differently, but you can see that you're now working with a list of dictionaries. You can access each value through list indexing followed by a dictionary lookup.

Practice

  • Give it a try and train your list indexing and dictionary lookup skills!
  • Write code to access the amount of folders that were collected during the first run of your file counter script.
  • Access the number of PNGs during the second run of the script.

After reading in your CSV data and converting it back to a familiar data type that you know how to work with, you're all set to analyze the changing contents of your desktop over time.

In the next lesson, you'll learn how to include Path() objects from the pathlib module into your File I/O operations.

Colorful illustration of a light bulb

Additional Resources

Summary: Open File and Read CSV Data

  • You can use a context manager, with, to read data from files in the same way as you used it before to write data to a file.
  • Python's csv module comes with some convenient abstractions, such as the csv.DictReader(), that you can use to create familiar data structures from your CSV data.