[Tutor] Writing a function to examine a directory and testing with unittest

Sukhvinder Singh laddosingh at gmail.com
Thu Sep 13 15:27:29 CEST 2012


I have an assigment where I'm need to write a module containing a function
to examine the contents of the current working directory and print out a
count of how many files have each extension (".txt", ".doc", etc.)

I am a beginner in Python.

This is the code of the function module:

import os
from collections import Counter

path = ":c//mypath/dir"
dirs = os.listdir( path )
filenames = {"this.txt", "that.txt",
extensions = []
for filename in filenames:
    f = open(filename, "w")
    f.write("Some text\n")
    name , ext = os.path.splitext(f.name)

# This would print all the files and directories
for file in dirs:

for ext, count in Counter(extensions).items():
    print("Count for %s: " % ext, count)


path is just an example - not real path.

I need to make this module into a function and write a separate unittest
module to verify by testing that the function gives correct results.

Help and pointers are much appreciated.

Sukhvinder Singh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120913/fa1b4b0b/attachment-0001.html>

More information about the Tutor mailing list