



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Files to submit: hw7.py Functions to include in hw7.py:
**1. compareNum()
The calendar will be more than helpful. Hint hint!!
Write a function compareNum that takes in a single parameter, a directory as a string. The first thing the function should do is examine whether or not the path is a directory. Then if it is, examine the path itself (do not go into subdirectories) and compare the number of files to folders. Your results should include the number of files and the numbers of folders. Print your results. Be sure to account for the case of an invalid path. Sample output:
>>> compareNum(“C:\Documents and Settings”) There are 5 folders and 28 files in the path C:\Documents and Settings There are more files than folders. >>> compareNum(“C:\Documents and Settings\user\My Documents”) There are 5 folders and 2 files in the path C:\Documents and Settings\user\My Documents There are more folders than files. >>> compareNum(“C:\Documents and Settings\user\”) There are 2 folders and 2 files in the path C:\Documents and Settings\user There are an equal number of folders and files. >>> compareNum(“C:\DocsSets”) Invalid Path
Write a function countFiles that takes in a single parameter, a directory as a string. First, the function should examine whether or not the path is a directory. If it is countFiles should examine all the files (not including subdirectories) in the directory and keep track of the number of .jpg, .txt, and .html files. Make sure that your function will take care of cases like .jpg and .JPG. Have your function print your results. Sample output: >>> countFiles(“C:\Documents and Settings”) .jpg: 5 .txt: 7 .html: 1 >>> countFiles(“C:\DocsSets”) Invalid Path
With all sorts of evil data mining robots out there to get you on the internet, it is important to protect your email address. We will use python to translate our email address into a common “safe” format. The email address [email protected] should be put into the format john[dot]smoo[at]gomail[dot]com. Here’s the catch— you MUST use split and join to do this. Other methods will not be accepted. Write a function, safeForWeb that takes in an email address as a string and returns the translated string. >>> safeForWeb("[email protected]") 'john[dot]smoo[at]gomail[dot]com'
(Wouldn’t that be nice!) This approach is called top-down programming. This function does not return anything. Here’s what makeHTMLPage() should do:
getPics(path) will look in a folder and generate html image tags for all files ending in “.jpg” or “.JPG” and look into any subfolders of the path to find pictures there as well. Then it will return the html code as a string. It assumes the path is given in the form of an empty string or that it ends with a directory structure delineator (“\” or “/” etc). We will add one more twist—we only want three pictures per row so it is necessary to throw in a
(break tag) for every three pictures that are written to the html file. To help you along, simply fill in the blanks on this code: def getPics(path): html = “” files = {list contents of path using os methods(s)} {for each item in files, you want to do the following} : {check if the item is a jpg} html += ‘’ {check if we have three pictures in the row yet} html += “
” elif os.path.isdir(path + item): html += {a recursive call with the new argument: path+item+os.sep } return {the thing that makes the most sense based on the above code}
You have a bunch of quotes written in separate text files and want them displayed to your webpage below your pictures. getQuotes(path) will look in a folder and generate html paragraph tags for all files ending in “.txt” or “.TXT” and look into any subfolders of the path to find pictures there as well. Then it will return the html code as a string. (Very similar to part 8).
You will need to read in the data from each text file (ex: text = filein.read() if filein is the file you have opened previously). Put this data in between HTML paragraph tags (
and
). Finally return the html code as a string. We have provided a sample folder for you to test your functions with—it should create a very entertaining picture if you do it correctly.Seriously.
Write a function numTypes that takes in a path as a parameter and returns the number of different types of files in the directory. Update the countFiles function so that it is recursive (looks into subdirectories).
Be sure to name your files and functions exactly as indicated. READ THROUGH THE INSTRUCTIONS CAREFULLY. If you need help, be sure to come to office hours. Good Luck! Written by Seung Mo Gu and Beth York, Fall 2007