[Tutor] inp. outp. & variable - dictionary difference ...

Alan Gauld alan.gauld at blueyonder.co.uk
Sun Nov 30 19:40:35 EST 2003


> 1.) First I found out, after trying few times, closing Python,
and
> opening it again, that sometimes it open file (for
reading/writting),
> and sometimes just don't,

I'm guessing but how do you start Python? Could it be that you
are
sometimes starting it from the folder that the filesw are stored
in
and sometimres from some place else? Python will look in
"the current directory" for the files if you don't provide the
full path.

> I have also tryed what Danny suggested, some other method to
open file, :
>
> >>> inp = open("D:/Program
Files/Python/Lib/idlelib/Saves/ggg.txt") ...
> and the file was opened normally !!
>
> ... but in the very next line (of course after typing
inp.close() ), with
> inp = open("somefile.txt","r") method:

Note you used the full path first time but a relatoive path
second time.
Suggests you were not in

D:/Program Files/Python/Lib/idlelib/Saves

when you started Python.

> 2.) Second about reading/writing from/into file, I noticed ...
> that Python doesn't want to write from one file to another,
> which is non-empty

I think what you mean here is that Python wipes out what was
previously in the file?
Thats what 'w' does. If you use 'a' it will append the new data
after the existing:

outp = ('ggg.txt','a')

> miss-spell something, and some error message appears - do I
need to
> close both (inp and out) files, and start from the beginning,
> or could I continue from the line before that error message ??

Depends on the error but for now its probably safer to close both
files
and start over.

> 3.) And as the last question, Alan.G's answer on my question
about
> "difference between different types of brackets", was:
>
> "{} indicate a dictionary. ... One item in the pair is the
lookup key,
> the other the return value."
>
> I am just curious - where is some "elemental" difference
between
> dictionary (in practical use) and simple variable,

Very observant. In fact Python uses dictionaries internally to
store variables.
That is a variable is just a key in a dictionary that Python uses
internally and
the value corresponding to the key is the variables value. In
fact the dir()
function lets you see the keys of the local dictionary (Or
whichever
dictionary/module you specify):

>>> import sys
>>> dir()
......lists local names...
>>> dir(sys)
.... lists names in sys module....

In both cases its just printing out the keys of the internal
dictionaries Python
is using. Thus

>>> sys.stdout

is a reference to a name in the dictionary used for the sys
module, and sys
in turn is a name in the local module.

Alan G.




More information about the Tutor mailing list