list index()

Hamilton, William whamil1 at entergy.com
Thu Aug 30 14:15:40 EDT 2007


> From: zzbbaadd at aol.com
> >
> > How could it not be an exception, in the plain English sense of the
> > word? Most certainly you're asking for the index because you want to
do
> > something with the index. If the item is not found, you have no
index,
> > so that's a special case that must be handled separately. There is
no
> > logical difference between handling that special case in an except
> > clause versus handling it with an if-branch.
> 
> In my case of have done os.listdir() on two directories. I want to see
> what files are in directory A that are not in directory B.
> I have used exceptions in other languages and only do so on logic that
> should never happen. In this case it is known that some of the files
> will not be in both lists. I just want to know which ones.
> 

I think you may be confusing exceptions and assertions.  Asserts are
generally used to trap conditions that should not happen, while
exceptions in Python are a standardized way to handle errors of all
sorts.  Where in C you would, say, open a file and check the return code
to ensure that the file actually exists before using it, in Python you
wrap the open statement in a try/except block instead.


--
-Bill Hamilton



More information about the Python-list mailing list