[Tutor] file methods
Alan Gauld
alan.gauld at btinternet.com
Tue Jul 10 09:15:26 CEST 2007
"bhaaluu" <bhaaluu at gmail.com> 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'
And this is where your problems start.
You are not creating a file object that you can use, instead you
are immediately calling read)() which reads the whole file as a
string. All the subsequent operations you do are to the string
not the file.
If you look at Johns origonal example he first created a file
object, f, and then experimented with the methods
Alan G.
More information about the Tutor
mailing list