Docsity

CS 1301 Homework 7: Python File Operations and Web Safety, Assignments of Computer Science

Toggle sidebar
bg1
Homework 7 CS 1301 – Fall 2007 Page 1/5
CS 1301 – Fall 2007
Homework 7 – Python Operates the System
Due: Friday, October 26, 2007 at 6 PM EST
Files to submit: hw7.py
Functions to include in hw7.py:
1. compareNum()
2. countFiles()
3. safeForWeb()
4. groceryList()
5. mySplit()
6. myJoin()
7. makeHTMLPage()
8. getPics()
9. getQuotes()
Parameters for functions will be noted in write-up below.
For Help:
T-Square Forum – see course syllabus for more detailed information
TA Helpdesk – schedule is posted on class website
Email TAs
Notes:
Think through the logic of each of the problems before you start coding anything
Don’t forget to include the required comments and collaboration statement (as outlined
on the course syllabus)
Do not wait until the last minute to do this assignment in case you run into problems
If you find an error in the homework assignment, let a TA know immediately
Information you might need
The calendar will be more than helpful. Hint hint!!
Part 1 – Number of Files vs. Number of Folders
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:
pf3
pf4
pf5

Partial preview of the text

Download CS 1301 Homework 7: Python File Operations and Web Safety and more Assignments Computer Science in PDF only on Docsity!

CS 1301 – Fall 2007

Homework 7 – Python Operates the System

Due: Friday, October 26, 2007 at 6 PM EST

Files to submit: hw7.py Functions to include in hw7.py:

**1. compareNum()

  1. countFiles()
  2. safeForWeb()
  3. groceryList()
  4. mySplit()
  5. myJoin()
  6. makeHTMLPage()
  7. getPics()
  8. getQuotes() Parameters for functions will be noted in write-up below.** For Help:  T-Square Forum – see course syllabus for more detailed information  TA Helpdesk – schedule is posted on class website  Email TAs Notes:  Think through the logic of each of the problems before you start coding anything  Don’t forget to include the required comments and collaboration statement (as outlined on the course syllabus)  Do not wait until the last minute to do this assignment in case you run into problems  If you find an error in the homework assignment, let a TA know immediately

Information you might need

The calendar will be more than helpful. Hint hint!!

Part 1 – Number of Files vs. Number of Folders

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

Part 2 – Count Files

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

Part 3 – Protecting your email address on the web

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'

Part 4 – Grocery List

(Wouldn’t that be nice!) This approach is called top-down programming. This function does not return anything. Here’s what makeHTMLPage() should do:

  1. Create a string containing an html header (ex: x = “”) Be sure to throw a title in there too.
  2. add the output of getPics() to the string from part 1
  3. add the output of getQuotes() to the string as well
  4. Add an html closing tag to the end of the string used in the first 3 parts (ex: x+= “”)
  5. open a file for writing called taawesomeness.html (this file does not need to exist beforehand) using the following as a guideline: myFile = open(“taawesomeness.html”, “w”) [the “w” is the “for writing” part]
  6. write the string containing the html code to the file using the file.write(str) method
  7. close the file you opened earlier

Part 8 – getPics

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}

Part 9 – getQuotes

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.

Part 10 – eatCake

Seriously.

Extra Credit:

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).

Final thoughts

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