Jesse Lawson

buy me a coffee ☕ / home / blog / tutorials / portfolio / contact

Mar 11, 2020 - Tutorials Python

Python Lecture Notes: Files and Recursion

These are some recipes that you might find useful for the Module 11 Assignment.

List all files in the current directory

import os 
# ...
for result in os.scandir(os.curdir):
    if result.is_file():
        print(result.name)

Remember to cd into the directory you want to list the files from.

What is recursion?

Recursion is achieved through the use of a function that calls itself. Technically, a recursive function will continue to call itself and do its thing until a specific condition is met.

One way to think about this is with a sandwich. Imagine an ordered list of sandwich parts:

sandwich = ['bread', 'lettuce', 'pickles', 'cheese', 'meat', 'bread']

Now imagine you are my son, who for some reason eats sandwiches top-down. So instead of taking a bite of everything at once, he’ll first eat the bread, then the lettuce, then the pickles… you get the idea.

We would have to write the function like this pseudocode:

1. Get the next element in the sandwich and eat it.
2a. If there is no next element:
2a.1. Quit eating.
2b. Otherwise:
2b.1. Go back to #1.

So in recursion we continue to loop through some set of logic that involves us re-calling that same set of logic over and over again.

For more, check out this tutorial on thinking recursively in Python. You may enjoy this more verbose example of the Santa Claus recursion function, too.

Copy a dictionary template to make a list of dictionaries

Let’s say you want to create a list of dictionaries:

import copy 

template_friend_dict = {
    "name": None,
    "age": None
}

my_friends = []

friend1 = dict(template_friend_dict)

Here we’re using the dict() function to create a shallow copy of template_friend_dict. You can read about the difference between shallow copy and deep copy here. In essence, the dict() function returns us a copy of the dictionary we pass to it.

Here is a full interactive example of how to make a list of dictionaries by copying a template dictionary.

Return a list of dictionaries

It’s common to store data as elements in a dictionary, and then those dictionaries in a list. Here’s how you would do that and then return it from a function:


my_friends = []

friend_template = {'name': None, 'age': None}

def get_friends():

    friends_list = []

    friend1 = dict(friend_template)
    friend1['name'] = "Jesse"
    friend1['age'] = 34
    friends_list.append(friend1)

    friend2 = dict(friend_template)
    friend2['name'] = "Dr. Waffles"
    friend2['age'] = 41
    friends_list.append(friend2)

    return friends_list

Get a list of all files in a folder

Let’s say you have a folder named pictures and you want to loop through all the files in that folder.

You can do that with the scandir() function delivered via the os module:

import os 

for result in os.scandir(os.curdir+"/pictures"):
    
    # Since scandir() will also return folder names, we check if 
    # result.is_file() == True to ensure we only grab files
    if result.is_file(): 
        print(f"Found file {result.name} with path {result.path})")

Notice how we get a .name and .path method–each of which return a string– attached to the interator object result. We can change the name of the iterator, variable of course.

Get a list of files in a folder that meet a condition

Let’s say you have a folder named ballot_initiatives, and in it, files that contain political ballot measure information brokend down by state such that each file starts with a two-character representation of a state.

For example:

\ballot_initiatives
    \CA-initiative1.txt
    \CA-initiative2.txt
    \AZ-initiative1.txt
    \TX-initiative1.txt
    \NY-initiative1.txt
    \NY-initiative2.txt

If we wanted to loop through and only get the initiatives for, say, California (which would start with a CA), we use a string slice on the name of the file:

import os 

print(f"Here are the California (CA) initiatives:")

for result in os.scandir(os.curdir+"/ballot_initiatives"):
        
    if result.is_file() and result.name[2:] == 'CA':
        print(result.name)

Move files from one folder to another

Let’s say you have a folder named unsorted and another folder named sorted. Inside sorted there are two subfolders: photos and videos.

Inside the unsorted folder are a bunch of files that are either JPGs or MP4s. As you may already know, JPGs are pictures and MP4s are videos.

You could automatically move the files from unsorted to their correct subfolder in sorted by using the shutil module’s move() function:

import shutil
import os 

# Loop through all files in `unsorted` directory
for result in os.scandir(os.curdir+"/unsorted"):
    if result.is_file(): # Ensure we are only getting files
        sub_folder = "photos" if result.name[:-3] == "JPG" else "videos"
        shutil.copy("unsorted/"+result.name, "sorted/"+sub_filder+"/"+result.name)

Write text to a file


with open('somefile.txt', 'w') as f:
    f.write("Hello, world!")

Read text from a file

new_file = ""
with open('somefile.txt', 'r') as f:
    num = 0
    for line in f:
        new_file += f"File line #{num}: {line}\n"
        num+=1

with open('someotherfile.txt', 'w') as f:
    f.write(new_file)