File write/read question

Bengt Richter bokr at oz.net
Fri May 17 13:45:08 EDT 2002


On 13 May 2002 10:07:11 -0700, nhellmers at yahoo.com (Nathan Hellmers) wrote:

>Hi,
>
>I'm just getting started with Python, and am stumped on a basic
>problem.  I have one program that runs a lot of calculations and
>produces an integer.  I want to write that integer to a file, and then
>read it with another program to run some more calculations.  I know
>that an integer written to a file is actually a string, but how do I
>convert the string to an integer once I have read it with program #2?
>
>Simplified example of what I am trying to do:
>
>---------------
>#1
>
>test = open("test.txt", "w")
>a = 1
>print >> test, a
>test.flush()
>----------------
>
>That successfully writes the number 1 to a file called test.txt.
>
>----------------
>#2
>
>test2 = open("test.txt", "r")
>a = test2.readline()
>int(a)
a=int(a) # otherwise the next statement still sees a as "1\n"
         # and prints the value of the expression "1\n" + "1\n" (which is "1\n1\n")
         # instead of the value of the expression 1 + 1
>print a + a
>----------------
>
>That successfully reads the number 1 from the file and assigns the
>variable "a" to it, but the printout is 1 1 (on two lines) rather than
>2.  Using type I can see that "a" was not converted to an integer.  I
It will probably help you in Python to start thinking of names and values
separately. IOW, "a" (w/o quotes) is a name, not a value. The thing that gets
converted is what is found when you look up the name and find out what value
it is bound to. In this case '1\n'. A name in Python is not a labeled storage slot
in the sense of C or assembly. It's better to think of names as keys to use for
looking up values in a dictionary of name->value associations (though implementation
varies in different contexts).

>can also see that "a" actually equals "a\n".  If I slice off the \n
                                  ITYM "1\n" ?
>with "a = a[0:1]", I still can't convert it to an integer.
>
int(a) converts, but doesn't rebind a to the integer resulting from the conversion,
unless you write a=int(a)

IOW, assignment in Python is re/binding, or changing the dictionary entry for
the name on the left hand side of the '=' so that lookup subsequently results
in the new value. When you write a=int(a), you don't modify the old value associated
with a, you create a new value and associate a with the new value, abandoning a's
claim on the old value (which gets garbage collected eventually).

If you write b=a before writing a=int(a), you are associating the name b with something,
namely the value associated with a ('1\n'). Thus that value gets two claims on it
(tracked by a reference count that is part of each value object). Thus if you then
rebind a with a=int(a), a's claim to its old value is abandoned, but not b's.
IOW, the reference count for '1\n' goes from 2 to 1, and a looks up the new value (1)
and b the old ('1\n').

>
>Any help would be greatly appreciated.
>
HTH

Regards,
Bengt Richter



More information about the Python-list mailing list