[Tutor] file methods
John Fouhy
john at fouhy.net
Tue Jul 10 01:00:56 CEST 2007
On 10/07/07, elis aeris <hunter92383 at gmail.com> wrote:
> from the document i know that if I want to open a text file I do:
>
> f = open("text.txt", "r+")
>
> and thus create f as an file object i can then use.
>
> however, i don't understand these functions
>
> .readline
> .readlines
> .read
> .xlinesread
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.
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.
--
John.
More information about the Tutor
mailing list