[Tutor] list index out of range error
Remco Gerlich
scarblac@pino.selwerd.nl
Wed, 21 Mar 2001 00:12:24 +0100
On Tue, Mar 20, 2001 at 06:03:27PM -0500, Shawhan, Douglas (GEAE, GECC) wrote:
> When I attempt to run the following:
>
> --------------begin yamo.py------------------------
> import glob
> import string
> gunther=open('/windows/desktop/merciful_wombat.html', 'w')
> yamo=glob.glob("/windows/desktop/*.html")
> a=0;b=100
> while a < b:
> gunther.write('<html><body
> bgcolor=white><table><tr><td>'+`yamo[a]`+'<tr><td
> bgcolor=#aaaaaa> </table>'),
> a=a+1
> gunther.close()
> os.system('iexplore.exe /windows/desktop/merciful_wombat.html')
>
> --------------end yamo.py---------------------------
>
> I get the following error:
>
> --------------erratta mondatta----------------------
>
> C:\WINDOWS\DESKTOP>python yamo.py
> Traceback (most recent call last):
> File "yamo.py", line 7, in ?
> gunther.write('<html><body
> bgcolor=white><table><tr><td>'+`yamo[a]`+'<tr><td
> bgcolor=#aaaaaa> </table>'),
> IndexError: list index out of range
>
> --------------end erratta mondatta----------------
>
> It writes the file "merciful_wombat.html" file just fine. But it gives me
> the error message all the same.
>
> Too many iterations in my while?
You do exactly 100 iterations. If the desktop has fewer html files, you get
that error; if it has more, you will omit some.
Change the while to 'while a < len(yamo)'.
Or even to 'for y in yamo' and use `y` in the html thingy instead of
`yamo[a]`.
> And another thing:
>
> import os
> os.system('notepad.exe')
>
> works fine, while
>
> import os
> os.system ('iexplore.exe')
>
> does not. Is there a path problem here?
I'll let someone else answer that since I don't use Windows.
--
Remco Gerlich