<div>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.</div>
<div><br></div><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div>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:</div>
</div><div><br></div><div>1) how to limit the iteration, and</div><div>2) how to accept a specified time period, and check/handle errors caused by incorrect input</div><div><br></div><div>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.</div>
<div><br></div><div>Thanks,</div><div>Cory</div><div><br></div><div><br><meta http-equiv="content-type" content="text/html; charset=utf-8"></div><div><div>def getpaths(dirpath):</div><div> yeardirs = os.listdir(dirpath)</div>
<div> filepaths = []</div><div> for yeardir in yeardirs:</div><div> yearpath = os.path.join(sitepath, yeardir)</div><div> if not os.path.isdir(yearpath): continue</div><div> mondirs = os.listdir(yearpath)</div>
<div> for mondir in mondirs:</div><div> monpath = os.path.join(yearpath, mondir)</div><div> files = os.listdir(monpath)</div><div> filepaths.extend([os.path.join(monpath,file) for file in files])</div>
<div> filepaths.sort()</div><div> return filepaths</div></div><div><br></div><div><br></div><div><br></div><br><div class="gmail_quote">On Tue, May 24, 2011 at 10:37 PM, Wayne Werner <span dir="ltr"><<a href="mailto:waynejwerner@gmail.com">waynejwerner@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="gmail_quote"><div class="im">On Tue, May 24, 2011 at 9:17 PM, Cory Teshera-Sterne <span dir="ltr"><<a href="mailto:ctsterne@gmail.com" target="_blank" class="vt-p">ctsterne@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>Hello,</div><div><br></div><div>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).</div>
</blockquote><div><br></div></div><div>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.</div>
<div class="im">
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>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.)</div>
</blockquote><div><br></div></div><div> Specifying a directory tree is quite simple - ask the user!</div><div><br></div><div>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:</div>
<div><br></div><div><div>>>> os.listdir('/home/flugle/')</div><div>Traceback (most recent call last):</div><div> File "<stdin>", line 1, in <module></div><div>OSError: [Errno 2] No such file or directory: '/home/flugle/'</div>
</div><div><br></div><div>Which you can handle in whatever way makes the most sense.</div><div><br></div><div>Alternatively, if you don't mind using Tkinter, you could do something like:<br></div><div><br></div><div>
import tkFileDialog as fg</div>
<div>import Tkinter as tk</div><div><br></div><div>root = tk.Tk()</div><div>root.withdraw()</div><div>filename = fd.askdirectory()</div><div>root.quit()</div><div># Go and do something with filename.</div><div><br></div>
<div>
HTH,</div><div>Wayne</div><div><br></div></div>
</blockquote></div><br>