Greetings,

On 7/9/07, John Fouhy <[EMAIL PROTECTED]> wrote:
>
> The best way to find out what the functions do is to experiment with them.
>
> eg:
>
> >>> f = open('text.txt', 'r')
> >>> f.readlines()
>
> and look at the output.

I like that idea. I made a simple english plain text file, thus:

first line
second line
third line

and named it text.txt.

I start the Python interactive interpreter:
>>>

>>> open('text.txt').read()
'first line\nsecond line\nthird line\n'

>>> len(open('text.txt').read())
34

>>> file=open('text.txt').read()
>>> print file
first line
second line
third line

>>> file.readlines()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'str' object has no attribute 'readlines'

> However, that said, the modern way to read a file one line at a time is:
>
> f = open('text.txt', 'r')
> for line in f:
>     # do something with line
>
> This will set the variable 'line' to each line of the file in turn.

>>> for line in file:
. . .        print line
. . .
f
i
r
s
t

l
i
n
e


s
e
c
o
n
d

l
i
n
e


t
h
i
r
d

l
i
n
e


>>>

>
> --
> John.

>>> Ctrl-D

Oh well, back to the tutorials.... 8^D
-- 
bhaaluu at gmail dot com
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to