problem with iteration

Discussion in 'Python' started by Panagiotis Atmatzidis, Nov 3, 2007.

  1. 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
     
    Panagiotis Atmatzidis, Nov 3, 2007
    #1
    1. Advertising

  2. Panagiotis Atmatzidis

    Paul Hankin Guest

    On Nov 3, 8:58 pm, Panagiotis Atmatzidis <>
    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
     
    Paul Hankin, Nov 3, 2007
    #2
    1. Advertising

  3. 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
     
    Bruno Desthuilliers, Nov 4, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    361
  2. Wijaya Edward
    Replies:
    1
    Views:
    241
    Marc 'BlackJack' Rintsch
    Oct 26, 2006
  3. Replies:
    7
    Views:
    236
    Arnaud Delobelle
    Apr 8, 2008
  4. cirfu
    Replies:
    2
    Views:
    242
    Matimus
    Jun 13, 2008
  5. Rudi
    Replies:
    5
    Views:
    5,144
Loading...

Share This Page