Packages provide additional tools and functions not present in base Python. Python includes a number of packages to start with, and others can be installed using pip install <package name>
and/or conda install <package name>
commands in your terminal.
Open your terminal by:
Once you've installed a package, you can load it into your current Python session with the import function. Otherwise these functions will not be available.
import os #functions for working with your operating system
import shutil #extra functions for working with files
Both of the packages above, os
and shutil
, are part of the "Python Standard Library" of packages and functions that come with every Python installation.
We don't load all of these packages every time we start Python for a couple of major reasons:
Note: If you need two functions with the same name, for example, fun
, from pkg1
and pkg2
we can always refer to them by their "full" names as: package-name.function-name
:
pkg1.fun()
pkg2.fun()
To open a file with Python, you'll need to tell your computer where it's located on your computer. You can specify the entire absolute filepath (starting with C:\ on PC or / on Mac), or you can set a working directory and work with relative file paths.
You can determine where a file is located on your computer by:
If a file is located in your working directory, its relative path is just the name of the file!
myfile="/Users/tuesday/Desktop/Python/Recipes.zip" #Mac absolute path
os.path.isfile(myfile) #check if Python can find my file
Windows Paths
Windows filepaths use \, which Python interprets as escape characters. This can be fixed in several ways:
Preface your path with r:
r"C:\Users\mtjansen\Desktop"
os.chdir("/Users/tuesday/Desktop/Python/") #set working directory
myfile="Recipes.zip" #relative path
os.path.isfile(myfile)
We can get a list of all files in the working directory with os.listdir(".").
print(os.listdir("."))
print(os.listdir("/Users/tuesday/Desktop/Python/")) #alternatively we can specify a folder
import os
and os.chdir
to set your working directory to the unzipped folder "Recipes". os.listdir
to check what files are stored in "Recipes".Python requires you both open and close files explicitly. If you forget to close a file, it can remain in use, preventing you from opening it later.
Best practices for reading and writing files use the with
function to make sure files are automatically closed.
os.chdir("/Users/tuesday/Desktop/Python/Recipes")
with open("amaranth-stirfry.txt","r") as txtfile: #"r" indicates that we are reading the textfile and not writing to it
recipe=txtfile.read() #.read() retrieves raw text information from the file we opened
print(recipe)
The recipe above is missing a serving amount. Lets add one in, and then save the file.
recipe = recipe + "Serves 4"
with open("amaranth-stirfry.txt","w") as txtfile: #"w" specifies that we're writing to the file
txtfile.write(recipe)
The Amaranth Stir Fry looks like it would make a nice hearty meal for fall. Let's create a new folder called "Fall" in our "Recipes" folder and put a copy of the Amaranth Stir Fry recipe inside it.
os.mkdir("Fall") #os.mkdir() creates a new folder
shutil.copyfile("amaranth-stirfry.txt", "Fall/amaranth-stirfry.txt") #shutil.copyfile() makes a copy
Did it work?
os.path.isfile("Fall/amaranth-stirfry.txt")
Great! We only have 199 more recipes to organize by season! Don't worry, though. This is a fast job for Python. We'll start by writing some pseudocode.
To organize our recipes, we'll need to...
Using a comprehension, make a list that contains all of our recipe files:
#this list comprehension makes sure we're only getting a list of our text files and our folder is not included
flist = [f for f in os.listdir("/Users/tuesday/Desktop/Python/Recipes") if f[-3:]=="txt"]
Create folders for the remaining seasons:
os.mkdir("Spring")
os.mkdir("Winter")
os.mkdir("Summer")
Create lists of ingredients for each season:
spring = ["asparagus", "cabbage", "cauliflower", "chard", "greens", "kale", "peas", "radish", "rhubarb", "strawberries", "turnip", "artichoke"]
summer = ["blackberries", "blueberries", "cantaloupe", "cherries", "cucumber", "eggplant", "beans", "melon", "okra", "peach", "plum", "raspberries", "strawberries", "watermelon", "zucchini", "apricot", "basil"]
fall = ["apple", "brussels sprouts", "cabbage", "cauliflower", "grapes", "mushrooms", "parsnip", "pear", "sweet potato", "pumpkin", "turnip", "rutabaga", "fig", "quince", "pomegranate", "chard", "greens", "kale", "butternut", "acorn", "cranberries"]
winter = ["grapefruit", "orange", "butternut", "acorn", "chestnut", "cranberries", "brussels sprouts", "cabbage", "cauliflower", "sweet potato", "pumpkin", "turnip", "rutabaga", "pomegranate", "chard", "greens", "kale"]
Notice that some ingredients fall into multiple seaons. Some of our recipes will also fall into multiple seasons.
Next, we need to combine all of our ingredient lists into a single dictionary so that we can loop through them later on.
seasons = {"Spring":spring, "Summer":summer, "Fall":fall, "Winter":winter}
Before we tackle the fourth step in our pseudocode, let's take a look at the recipe for Apple Carrot Muffins. We'll practice classifying just this recipe first.
fname = "apple-carrot-muffins.txt" #Store the file name in a variable called "fname"
with open(fname,"r") as txtfile:
recipe=txtfile.read()
print(recipe)
What season will this recipe fall into? To find out, we need to build some nested loops. Remember our pseudocode?
for s in seasons:
for ingredient in seasons[s]:
if (ingredient in recipe.lower()): #.lower() changes all text in the recipe to lowercase
shutil.copyfile(fname, os.path.join(s, fname)) #os.path.join() joins the folder name with the file name
Now we can use os.list()
to find out which folder the recipe was placed in.
for s in seasons: #Loop through each season folder
print(s) #Print the folder name
print(os.listdir(s)) #List the files in the folder.
flist
and open each file.If you're having a lot of trouble with this exercise, one possible solution can be found here.