[Tutor] Functions and lists.

alan.gauld@bt.com alan.gauld@bt.com
Wed, 14 Aug 2002 15:15:05 +0100


> list = os.listdir("%s" % value)

list = os.listdir(value) # seems less cumbersome...

> def input(insource):
>     infile = open(value + insource, "rb")
>     text = infile.read()
>     infile.close()
>     return text

redefining the builtin input() function probably 
isn't a good idea...but it should work...

> In the main namespace I have a iteration that goes like so:

> for i in range(l):
>     input(list[i])

But you don't assign the return value of input() to anbythiong, you just
throw it waay. You eed:

       txt = input(list[i])
       substr(txt)

> Sorry, substr is another function that does some re on text.

You might need to catch the output of that too if 
you want to display it say...

> The problem lies in the function input. Does return not 
> return 'text' to the top-level namespace 

I think it does, its just that you aren't assigning 
it to anything! Its the *value* of text thats returned 
not the name.

> Traceback (most recent call last):
>   File "./remodpyqt.py", line 29, in ?
>     substr(text)
> NameError: name 'text' is not defined

Just so. text is a name that is only known inside input().

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld