[Tutor] a quick Q: how to use for loop to read a series of files with .doc end
Andreas Perstinger
andreas.perstinger at gmx.net
Fri Oct 7 15:08:00 CEST 2011
On 2011-10-07 14:21, lina wrote:
>>> I don't know why this gives a key error on 'E' (which basically means that
>>> there is no key 'E') since the code above should guarantee that it exists.
>>> Odd. I'm also not sure why the error occurs after it prints summary. Are you
>>> sure the output is in the sequence you showed in your message?
>>>
>>> One simple explanation: it continued on to the next file, which has
>> neither "E" nor "B" in it.
>>
> In this directory, I only kept one file. try.xpm
That's wrong.
> $ more try.xpm
> aaEbb
> aEEbb
> EaEbb
> EaEbE
>
> $ ls
> counter-vertically-v2.py try.xpm
> counter-vertically.py try.txt
As "ls" proves, you have *4* files in the directory.
You start your script with "dofiles(".")". This function gets a list of
*all* files in the directory in an *arbitrary* order and processes each
of it.
In your function "processfile" you first create an empty dictionary
("results = {}") and then you put values into the dictionary *only* for
xpm-files ("if ext == INFILEEXT:"). *But* you print the summary for
*every* file because the indentation at the end of the function is
outside the if-branch where you check for the file extension. So for
every file which isn't a xpm-file, "results" is an empty dictionary (see
first line of the function) and therefore "zip(results['E'],
results['B'])" will throw an exception.
How to solve it? I personally would check for the file extension in the
function "dofiles" and would only continue with xpm-files (in other
words move the if-statement from "processfile" to "dofiles".)
Bye, Andreas
More information about the Tutor
mailing list