problem with iteration

  • Thread starter Panagiotis Atmatzidis
  • Start date
P

Panagiotis Atmatzidis

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
 
P

Paul Hankin

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.
 
B

Bruno Desthuilliers

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top