[Tutor] Need help wrapping my head around duck typing

Cory Teshera-Sterne ctsterne at gmail.com
Wed May 25 04:57:53 CEST 2011


Hmm ... I thought I was going to try to keep this as general as possible,
but it may be more helpful if I gave a more concrete example. The code below
is the existing function; I'd be ok with throwing it out & starting over,
but originally thought I could modify it fairly easily.

The directory structure this function works on contains directories named as
years (2010, 2009, 2008, ...), each containing month directories (01, 02,
03, ...), which in turn contain files (I can assume the files' extension,
but not their creation date). I'm trying to modify the function so that it
only returns filepaths from a specified time period (ie, May '10 to Feb '11,
or, in directories, the contents of  /2010/05, /06 ..., then /2011/01, 02).
This means I have two problems:

1) how to limit the iteration, and
2) how to accept a specified time period, and check/handle errors caused by
incorrect input

I think I've figured out #1 but would appreciate suggestions; #2 is really
throwing me for a loop & exposing a lot that I don't know about Python.

Thanks,
Cory


def getpaths(dirpath):
    yeardirs = os.listdir(dirpath)
    filepaths = []
    for yeardir in yeardirs:
        yearpath = os.path.join(sitepath, yeardir)
        if not os.path.isdir(yearpath): continue
        mondirs = os.listdir(yearpath)
        for mondir in mondirs:
            monpath = os.path.join(yearpath, mondir)
            files = os.listdir(monpath)
            filepaths.extend([os.path.join(monpath,file) for file in files])
    filepaths.sort()
    return filepaths




On Tue, May 24, 2011 at 10:37 PM, Wayne Werner <waynejwerner at gmail.com>wrote:

> On Tue, May 24, 2011 at 9:17 PM, Cory Teshera-Sterne <ctsterne at gmail.com>wrote:
>
>> Hello,
>>
>> Thanks for the input. I guess you're right, this is more of a case of
>> argument assertion - but then I'm not sure how to do error handling here,
>> because, for example, multiple issues that should be dealt with in very
>> different ways could raise the same error (as in the original example).
>>
>
> Well, there are (usually) built-in exceptions that work quite well. Wrong
> type? Raise a TypeError. Value out of bounds? Raise a ValueError. Usually it
> turns out that if you're raising more than one error, you probably have your
> function doing too many things.
>
> I'm also not sure how using glob would work any differently here - wouldn't
>> I still need to iterate over a specified section the directory tree, and
>> therefore have to figure out how to specify it? (Admittedly, I've only
>> played with it for a few minutes, so I might be missing something obvious -
>> and file creation dates don't mean anything in this particular context.)
>>
>
>  Specifying a directory tree is quite simple - ask the user!
>
> Then you assume that the directory is correct/exists and you check the
> files, either with glob or os.listdir, and return a list of anything that
> matches. In the case that they enter an incorrect directory? If you use
> glob, you'll get nothing. If you use os.listdir, you'll get something like
> this:
>
> >>> os.listdir('/home/flugle/')
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> OSError: [Errno 2] No such file or directory: '/home/flugle/'
>
> Which you can handle in whatever way makes the most sense.
>
> Alternatively, if you don't mind using Tkinter, you could do something
> like:
>
> import tkFileDialog as fg
> import Tkinter as tk
>
> root = tk.Tk()
> root.withdraw()
> filename = fd.askdirectory()
> root.quit()
> # Go and do something with filename.
>
>  HTH,
> Wayne
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110524/05bbb6f2/attachment.html>


More information about the Tutor mailing list