"elis aeris" <[EMAIL PROTECTED]> wrote

> python 3.9 File Objects  of Python Library Reference
>

Learning to program from the reference manual is possible,
especially if you use the interpreter to experiment as John
has suggested. But it will be much faster and less error prone
if you just work through oine of the beginners tutorials. They
will all explain how to access files.

> from the document i know that if I want to open a text file I do:
>
> f = open("text.txt", "r+")

Usually you don't want the + sign which means "read and write".
You open a file to read or to write, very rarely do you open it to
read and write at the same time, that's frought with difficulty,
especially for a beginner.

Thus the best way to open a file is either

f = open('text.txt','r')   # to read it
f = open('text.txt','w') # to create a new file and write to it

> and thus create f as an file object i can then use.
>
> however, i don't understand these functions
>
> .readline

This reads a line of the file

> .readlines

 this reads all of the lines in the file into a list

> .read

this reads the whole file as a single string

> .xlinesread

I assume you mean xreadlines?

Thats pretty much not needed nowadays, it used
to be a more memory efficient version of readlines

> I have a file like this one:
>
> command = func_babara
> parameter_1 = 300
> parameter_2 = 300
> parameter_3 = 50
> parameter_4 = 0
> parameter_5 = 0
> parameter_6 = 0
>
>
> ,as you see, i need to process it one line at a time and use this 
> .ini file
> as a configuration file.

OK, Just use readlines to read each line and process it.
Or as John mentions just iterate over the file directly.

> how do I use those functions, I don't understand the libarry 
> reference.

Read one of the tutorials, such as the Handling Files topic in my 
tutorial.

> also, I need to output lines like above to text.txt,  how do I do 
> it?

Use the write methods, you have only looked at the read ones
from your list above. Again see the Handling Files topic in my
tutorial, or indeed any other beginners tutiorial.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to