[Tutor] Stuck on Something
David Porter
jcm@bigskytel.com
Wed, 7 Mar 2001 20:15:38 -0700
* Britt Green <britt_green@hotmail.com>:
> >#---
> >
> >import string
> >
> >raw_dirs = """\ drwxrwx--- 2 restback privftp 512 Mar 6 21:23
> >technicolorkitchen drwxrwx--- 2 restback privftp 512 Mar 6 21:58
> >theclubhouse drwxrwx--- 2 restback privftp 512 Mar 6 21:07 thirdstreet
> >drwxrwx--- 2 restback privftp 512 Feb 27 11:18 thyme"""
> >
> >dirs = [] raw_dirs = string.split(raw_dirs, '\n') for x in raw_dirs:
> >dirs.append(string.split(x)[-1])
> >
> >print dirs
>
> Okay. I see what you're doing with this. Unfortunately, when I run this I
> get this error:
>
> >>>
> Traceback (innermost last):
> File "C:\Program Files\Python20\Code\anotherftpthing.py", line 22, in ?
> dirs.append(string.split(x)[-1])
> IndexError: list index out of range
Ah. This could be due to empty lines in raw_dirs. How about changing
for x in raw_dirs:
dirs.append(string.split(x)[-1])
TO
for x in raw_dirs:
try:
dirs.append(string.split(x)[-1])
except IndexError:
pass
This *should* ignore any line without a directory. Another method will trim
trailing and leading whitespace which might address the problem more
directly. just add the line:
raw_dirs = string.strip(raw_dirs)
right before
raw_dirs = string.split(raw_dirs, '\n')
HTH,
David