File write/read question

Mark McEahern marklists at mceahern.com
Mon May 13 13:28:15 EDT 2002


[Nathan Hellmers]
>#2
>
>test2 = open("test.txt", "r")
>a = test2.readline()
>int(a)
>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
>can also see that "a" actually equals "a\n".  If I slice off the \n
>with "a = a[0:1]", I still can't convert it to an integer.

Strings are immutable so int(a) returns an int--but you don't bind it to
anything.  Try this instead:

  a = int(a)

Cheers,

// mark






More information about the Python-list mailing list