Adding Loop in the File Command Python -


ı wanna create file :

x values:

1 2 3 4 5 . . . 999 

to ı wrote command ;

error :argument 1 must 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 

when writing file, must write strings trying write floating point value. use str() turn strings writing:

c.write(str(count)) 

note c.close line nothing, really. refers .close() method on file object not invoke it. neither want close file during loop. instead, use file context manager close automatically when d one. need include newlines, explicitly, writing file not include print statement would:

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

note incrementing counter 0.1, not 1, creating 10 times more entries question seems suggest want. if wanted write integers between 1 , 999, may use xrange() loop:

with open("text.txt","w") c:     count in xrange(1, 1000):         print count         c.write(str(count) + '\n') 

Comments