[Tutor] file.readlines() error

alan.gauld@bt.com alan.gauld@bt.com
Fri, 11 Oct 2002 08:59:10 +0100


> See underneath the code for the error...
> 
> def tagfinger():
>     import sys, string, os, os.path, glob, re
....
>     print "The pathname you have chosen is: ",path_in
>     print ""
>     from os import chdir


Warning this is ILLEGAL CODE in Python.
You are not supposed to use fom X import Y inside a function.
In Python 2.x it will spit out a warning or evben an error.
But even in previous versions it is an error.

See the python idioms paper in the topics section of 
the python web site....

>     rtfile=file(answer, 'r')
>     print '...Reading...',answer
>     rtfile=file.readlines()  ### <<<------ Error because I 

It looks like you are getting some kind of conflict between
the os and builtin file handling functions. This might be 
a bad result of the from/import above. Try taking out 
(or moving it to global level) the from/import and see 
what happens.

> TypeError: descriptor 'readlines' of 'file' object needs an argument

My theory is based on the word 'descriptor'...

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