[Tutor] file methods

bhaaluu bhaaluu at gmail.com
Tue Jul 10 14:24:36 CEST 2007


Greetings,

On 7/9/07, John Fouhy <john at fouhy.net> wrote:
> On 10/07/07, bhaaluu <bhaaluu at gmail.com> wrote:
> > >>> file.readlines()
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in ?
> > AttributeError: 'str' object has no attribute 'readlines'
>
> This error here is caused by this earlier statement:
>
> > >>> file=open('text.txt').read()
>
> 'file' is now a string, not a file-like object.  This also causes the
> behaviour you get when iterating 'for line in file'...
>
> --
> John.
>

Thank you for the clarification.
One pitfall of experimentation as a Noob is in not knowing enough to
figure out what or why errors are generated. Thus, this Tutor list is very
helpful. I really appreciate the feedback.

So, in summary:

file = open('text.txt', 'r')

makes 'file' a "file object".

and

file = open('text.txt').read()

makes 'file' hold 'text.txt' as a string.

If 'file' is an 'object', I guess it is easier to use the "methods"
.readline(), .readlines() ,.read(), and .xlinesread(), on it?

Whereas, if 'file' is a string, those "methods" can't be used on it?

I'm still struggling with the oop terminology, so please be gentle. =)

As regards the terminology: a problem for Me is that the OOP terms
for Python are usually explained in terms of other OOP languages.
I would certainly welcome an introduction to OOP for Python that
explains OOP without referring to any other OOP language. Python
is my first OOP language, and the references to what the concepts
are called in other OOP languages are just plain confusing at times.
My modus operandi :
Read from a Python tutorial.
Type in the examples and run them.
Read more, type more, run more.
Read the Python Tutor mailing list....

Evidently, in Python, the main programming paradigm is:
object.attribute
AND
in Python, "everything is an object."

Cheers! 8^D
-- 
bhaaluu at gmail dot com


More information about the Tutor mailing list