I don't seem to be able to save a number to a data file:

a = 123.44
f = open("data1.dat", "w")
f.write(a)
f.close()

I get this message:

Traceback (most recent call last):
  File "<string>", line 204, in run_nodebug
  File "C:\Python25\ZLM\int2file.py", line 4, in <module>
    f.write(a)
TypeError: argument 1 must be string or read-only character buffer, not float

I know I must overlook something simple.

Hi!

Python usually produces very useful error-messages, like here

TypeError: argument 1 must be string or read-only character buffer, not float

Ok, you know what to do, right? Just convert your number to a string ;)

a = 123.44
f = open("data1.dat", "w")
f.write( str(a) )     # str() converts to string
f.close()

Regards, mawe

Thanks! So write() only takes a string argument. When i read the data back, do i have to use float()?

Sorry, i am really knew to Python!

When i read the data back, do i have to use float()?

That's right. You read the data always as a string, so you have to convert it by hand to whatever you need.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.