newbie: strange python problem

Michael Janssen Janssen at
Thu Apr 10 21:02:46 CEST 2003

maxx wrote:

> Simple code (I think):
> ---
> #!/usr/bin/python
> import os
> for line in os.popen('ls -la /').readlines():
>     direntry = line.split()
>     print direntry
>     print direntry[0]
>     print direntry[1]
>     print direntry[2]
> ---

beside to not trust the output of "ls" you should also write:
for field in line.split()[:3]:
    print field

this way you don't need to hardcode the slices and a "range-slice" like 
[:3] can't fail. The for-loop produce three fields of output or less if 
less is given (including no output at all).


More information about the Python-list mailing list