Computer Sciences Department logo

CS 368-3 (2012 Summer) — Day 6 Homework

Due Monday, July 9, at the start of class.


Print detailed information about a directory, its subdirectories, and its files, recursively. This script is fairly simple to write, IF you make good use of built-in functions and modules.


Your script must accept as input one directory path — either ask the user to type in the directory or simply hard-code it into your script.

In general, a directory may contain regular files and subdirectories (and other unusual things, which you do not need to worry about for this assignment). This definition is recursive: subdirectories may contain files and further subdirectories, which may contain files and subdirectories of subdirectories, and so forth.

For the initial directory and each of its subdirectories, sub-subdirectories, and so forth, print the following information:

You may assume that there are only regular files and directories.

Take care that your script works for any starting directory, which may contain no files, no subdirectories, or lots of each.

Example Output

Directory </p/course/cs368-cat/public/html/2010>
        9641   2010-07-01 16:25:22   resources.html
        7068   2010-07-20 13:59:20   index.html
        1541   2009-07-08 09:52:37   cs-logo.gif
         707   2010-07-20 16:16:16
         658   2009-07-10 15:49:32   368-3.css

Directory </p/course/cs368-cat/public/html/2010/01-introduction>
      115281   2010-07-12 10:18:22   cs368-3-2010-summer-01-0712M.pdf
        2369   2010-07-12 09:51:49   homework-01-assignment.html

Directory </p/course/cs368-cat/public/html/2010/02-syntax>
      131814   2010-07-13 09:56:27   cs368-3-2010-summer-02-0713T.pdf
        2974   2010-07-13 10:02:07   homework-02-assignment.html
         642   2010-07-13 16:40:02   in-class-code.html

Directory </p/course/cs368-cat/public/html/2010/03-collections>
      114186   2010-07-15 09:44:21   cs368-3-2010-summer-03-0715R.pdf
        4144   2010-07-15 09:53:12   homework-03-assignment.html

Directory </p/course/cs368-cat/public/html/2010/04-basic-io>
     1454670   2009-07-13 13:46:40   homework-04.txt
      132377   2010-07-16 10:12:51   cs368-3-2010-summer-04-0716F.pdf
        3389   2010-07-16 10:34:29   homework-04-assignment.html

Directory </p/course/cs368-cat/public/html/2010/05-subroutines>
      113456   2010-07-19 10:08:07   cs368-3-2010-summer-05-0719M.pdf
        3213   2010-07-19 10:30:29   homework-05-assignment.html
        1133   2010-07-19 14:39:02   in-class-code.html

Directory </p/course/cs368-cat/public/html/2010/06-data-structures>
      237215   2009-07-20 23:57:39   example_access_log
      124530   2010-07-20 09:49:22   cs368-3-2010-summer-06-0720T.pdf
        8092   2010-07-20 10:14:27   homework-06-assignment.html

Directory </p/course/cs368-cat/public/html/2010/08-standard-library>
        2484   2010-07-20 16:15:22   homework-08-assignment.html


The following hints are a bit … vague. I am doing this to encourage you to look up some functions and modules online. You need to learn to do this anyway, so consider it good practice.

First, here is a list of functions (including some in the standard modules) that might be useful. All were mentioned in the class slides so far. Not all of them will be useful for this assignment, but it is good to know about them anyway. If your script reinvents any of these functions, you may not receive full credit for your assignment!!!

And here is an organizational hint. I suggest organizing your script in two parts: First, build a list of all directories and subdirectories, and then, for each one, print the report of the regular files contained therein. This is not the only way to do this assignment, but I find it is conceptually clear.


Here are some specific tests to consider:

Extra Challenges


Do the work yourself, consulting reasonable reference materials as needed. Any resource that provides a complete solution or offers significant material assistance toward a solution not OK to use. Asking the instructor for help is OK, asking other students for help is not. All standard UW policies concerning student conduct (esp. UWS 14) and information technology apply to this course and assignment.

Hand In

A printout of your code, ideally on a single sheet of paper. Be sure to put your own name in the initial comment block. Identifying your work is important, or you may not receive appropriate credit.