views:

114

answers:

4

i wrote a simple function to write into a text file. like this,

def write_func(var):
        var = str(var)
        myfile.write(var)
a= 5
b= 5
c= a + b
write_func(c)

this will write the output to a desired file. now, i want the output in another format. say,

write_func("Output is :"+c)

so that the output will have a meaningful name in the file. how do i do it? and why is that i cant write an integer to a file? i do, int = str(int) before writing to a file?

+2  A: 

Simple, you do:

write_func('Output is' + str(c))

You have to convert c to a string before you can concatenate it with another string. Then you can also take off the:

var = str(var)

From your function.


why is that i cant write an integer to a file? i do, int = str(int) before writing to a file?

You can write binary data to a file, but byte representations of numbers aren't really human readable. -2 for example is 0xfffffffe in a 2's complement 32-bit integer. It's even worse when the number is a float: 2.1 is 0x40066666.

If you plan on having a human-readable file, you need to human-readable characters on them. In an ASCII file '0.5' isn't a number (at least not as a computer understands numbers), but instead the characters '0', '.' and '5'. And that's why you need convert your numbers to strings.

NullUserException
I do not agree with human readability argument. In fact the `str` that `write` accepts is a byte string. It can be a 4-byte representation of the (short) int just as well (you can obtain it using `struct` module).
Constantin
@Constantin OK, do you prefer `0x3F000000` or 0.5?
NullUserException
I think the argument should go: Why can't you write an integer to a file? Because `write` takes a series of bytes. If you want binary data, then give it binary data. If you want readable/ASCII/unicode (as OP wants), then give it printable bytes. Files contain whatever you specify. :P
Nick T
@NullUserException, sometimes i prefer 0x3F000000. Still, you give the wrong reason to the design decision behind the fact that `file.write` in Python accepts byte strings, but not ints or other arbitrary objects. It has nothing to do with readability of resulting file.
Constantin
@Constantin, Nick: Good points, I amended my answer.
NullUserException
Ugh I can't get the wording right. Feel free to edit my answer.
NullUserException
@NullUserException: Ok, the *real* reason is because writing a number, or even integer to a file is totally ambiguous. Files could go anywhere, to be read by anything. It could be stored as a `uint8`, a `uint16`, a `uint32`, a `uint64`, a `Bignum` (Python long), or maybe even a `Decimal`, `float` or `double` (`quad`?). If you want something Python specific, `pickle` is the tool to use
Nick T
+2  A: 

You can't add/concatenate a string and integer directly.

If you do anything more complicated than "string :"+str(number), I would strongly recommend using string formatting:

write_func('Output is: %i' % (c))
Nick T
+1  A: 

From http://docs.python.org/library/stdtypes.html#file.write

file.write(str)

Write a string to the file. There is no return value. Due to buffering, the string may not actually show up in the file until the flush() or close() method is called.

Note how documentation specifies that write's argument must be a string.

So you should create a string yourself before passing it to file.write().

Constantin
+2  A: 

Python is a strongly typed language. This means, among other things, that you cannot concatenate a string and an integer. Therefore you'll have to convert the integer to string before concatenating. This can be done using a format string (as Nick T suggested) or passing the integer to the built in str function (as NullUserException suggested).

Manoj Govindan