Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > problem with iteration

Reply
Thread Tools

problem with iteration

 
 
Panagiotis Atmatzidis
Guest
Posts: n/a
 
      11-03-2007
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]

----------------

I'm learning through practice and this is my first program. The error
may seem easy for you. However except from the explanation I'd like to
know how can I handle situations like the one above. I tought that
itering was valid there

Regards,

atma

 
Reply With Quote
 
 
 
 
Paul Hankin
Guest
Posts: n/a
 
      11-03-2007
On Nov 3, 8:58 pm, Panagiotis Atmatzidis <(E-Mail Removed)>
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


 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      11-04-2007
Panagiotis Atmatzidis a écrit :
> 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:


if html_files is None:

> print "No html files found in your path."
> else:
> for oldfile in html_files: <-- HERE IS THE ERROR
> [rest of code here]


>
> I'm learning through practice and this is my first program. The error
> may seem easy for you.


Indeed !-)

> However except from the explanation I'd like to
> know how can I handle situations like the one above. I tought that
> itering was valid there


Obviously not. You may want to learn more about the re module, and what
re.search returns. Anyway, if what you want is to find out if some file
name contains the string 'html', you don't need regexps:

if 'html' in 'index.html':
print "hurray"

Now I'm not sure you quite get how os.walk works - here, 'files' is a
list of file *names*, so I don't see how you could expect one of it's
elements to itself become a list of file names !-)

IOW, assuming you want to detect file names ending with '.html' in a
given part of your directory tree, the following code might do:

def injectCode(path, statcode):
html_files = []
for root, dir, files in os.walk(path, topdown=True):
for name in files:
if name.endswith('.html'):
html_files.append(os.path.join(root, name))
if not html_files:
print "No html files found in your path."
else:
for html_file in html_files:
[rest of code here]

HTH
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Struts - Problem with nested iteration or double iteration Rudi Java 5 10-01-2008 03:30 AM
weird iteration/assignment problem cirfu Python 2 06-13-2008 04:55 PM
A file iteration question/problem tinnews@isbd.co.uk Python 7 04-08-2008 05:38 AM
Problem Commenting within Filehandle Iteration Wijaya Edward Python 1 10-26-2006 08:18 AM
Strange problem with Tkinter... photos don't show on first iteration. aldonnelley@gmail.com Python 3 08-14-2006 12:42 PM



Advertisments