Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

ı wanna create a file which as :

X values:

1
2
3
4
5
.
.
.
999

to do that ı wrote that command but ;

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

from numpy import *

c = open("text.txt","w")
count = 0
while (count < 100):
   print  count
   count = count + 0.1
   c.write (count)
   c.close
share|improve this question
    
Wouldn't you want count = count + 1, in any case? –  chepner Jul 15 '13 at 12:35

3 Answers 3

up vote 6 down vote accepted

When writing to a file, you must write strings but you are trying to write a floating point value. Use str() to turn those into strings for writing:

c.write(str(count))

Note that your c.close line does nothing, really. It refers to the .close() method on the file object but does not actually invoke it. Neither would you want to close the file during the loop. Instead, use the file as a context manager to close it automatically when you are d one. You also need to include newlines, explicitly, writing to a file does not include those like a print statement would:

with open("text.txt","w") as c:
    count = 0
    while count < 100:
        print count
        count += 0.1
        c.write(str(count) + '\n')

Note that you are incrementing the counter by 0.1, not 1, so you are creating 10 times more entries than your question seems to suggest you want. If you really wanted to only write integers between 1 and 999, you may as well use a xrange() loop:

with open("text.txt","w") as c:
    for count in xrange(1, 1000):
        print count
        c.write(str(count) + '\n')
share|improve this answer
    
But he want to increase one by one –  Ugur Jul 15 '13 at 11:50
    
@Ugur: That is a separate issue. –  Martijn Pieters Jul 15 '13 at 11:51
    
Only answer that fixes everything. +1 –  Haidro Jul 15 '13 at 11:53
    
thank u.. now thats clear for me . ı wanna have one addıtıonal question how can ı over write random float Values, over that file with commands,, for example X 1 2 3 and ı wanna over wrıte like ; X Y 1 2.5 2 3.6 –  nuri_ Jul 15 '13 at 12:40
    
Write a new file, then replace the old file with that new one. –  Martijn Pieters Jul 15 '13 at 12:51

Multiple problems which I can see are : 1. You can only write character buffer to file. The solution to main question u asked.

c.write (count) should be c.write (str(count))

2. You need to close your file outside the loop. You need to unindent c.close

from numpy import *    
c = open("text.txt","w")
count = 0
while (count < 100):
   print  count
   count = count + 0.1
   c.write (count)
c.close()

3. Even after these this code will print and save numbers incremented with 0.1 i.e 0.1,0.2,0.3....98.8,99.9 You can use xrange to solve your problem.

result='\n'.join([str(k) for k in xrange(1,1000)])
print result
c = open("text.txt","w")
c.write(result)
c.close()
share|improve this answer

also, you're closing your file on each iteration of the while loop, so this will write your first line and then crash.

Unindent your last line so that the file's only closed after everything's been written to it:

while (count < 100):
    print  count
    count = count + 0.1
    c.write(str(count))
c.close()
share|improve this answer
    
No, the c.close does nothing. It is a no-op. It references the method without calling it. –  Martijn Pieters Jul 15 '13 at 11:52
    
right, i focused on the indentation and completely overlooked this. I'll edit and fix my answer. –  astrognocci Jul 15 '13 at 11:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.