problem with iteration
Paul Hankin
paul.hankin at gmail.com
Sat Nov 3 17:40:25 EDT 2007
On Nov 3, 8:58 pm, Panagiotis Atmatzidis <p.atmatzi... at gmail.com>
wrote:
> Hello,
>
> I managed to write some code in order to do what I wanted: Inject code
> in the right place, in some html files. I developed the program using
> small functions, one at the time in order to see how they work. When I
> tried to put these pieces of code together I got this error:
> TypeError: iteration over non-sequence
>
> Here is the code snippet that has the issue
>
> --------------
> def injectCode(path, statcode):
> for root, dir, files in os.walk(path, topdown=True):
> for name in files:
> html_files = re.search("html", name, flags=0)
> if html_files == None:
> print "No html files found in your path."
> else:
> for oldfile in html_files: <-- HERE IS THE ERROR
> [rest of code here]
First step in debugging is to look at the error, and work out what's
going on. 'iteration over non-sequence' suggests that 'html_files'
isn't a sequence. If it were my program and I didn't understand, I
might add 'print html_files' before the offending line to see what it
is. That would have given something like:
<_sre.SRE_Match object at 0x86608>
Not particularly helpful, but 'sre' and 'Match' suggests something to
do with regular expressions. Given the variable name, you obviously
expected a list of files, so you could look at where html_files is
created (with a re.match call) to see why your expectation differs
from what happened. Perhaps you could add more print statements to the
loops to see what values 'files' and 'name' take.
--
Paul Hankin
More information about the Python-list
mailing list