Help to convert Number to String

Vamsi vamsikrishna.bandlamudi at gmail.com
Fri Aug 13 22:45:11 EDT 2010


On Aug 13, 9:06 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Fri, 13 Aug 2010 13:50:48 -0700, Vamsi wrote:
> > I am trying to count the number of lines in a file and insert  into the
> > file but getting the error message "TypeError: must be string or
> > read-only character buffer, not int", Could you please help me how to
> > correct this?
>
> > here is the code
>
> > lines1 = sum(1 for line in open('C:/test1.txt'))
> > wfile = open('C:/test1.txt'', 'a')
> > wfile.write(str(lines1).zfill(9))
> > wfile.close()
>
> No, that ISN'T the code you are using.
>
> Don't re-type the code (introducing syntax errors), but copy and paste
> WORKING code. Also you need to copy and paste the EXACT error message you
> get, not just paraphrasing it.
>
> When I correct the obvious errors in your code above, it works for me:
>
>
>
> >>> f = open('test', 'w')
> >>> f.write('hello\nworld\n')
> 12
> >>> f.close()
> >>> lines1 = sum(1 for line in open('test'))
> >>> wfile = open('test', 'a')
> >>> wfile.write(str(lines1).zfill(9))
> 9
> >>> wfile.close()
>
> and the file is correctly updated:
>
> >>> open('test').read()
>
> 'hello\nworld\n000000002'
>
> --
> Steven

Thank you Steve for your response.
I pasted only part of the code.I am new to Python and
using 2.7.My actual code is as below, If I run the below code I am
getting the error "TypeError: 'str' object is not callable" ,Now I
found that I am using the "str" variable which is  a function.Thanks
a
lot for your help.
fileopen = open('C:/MPython/test.txt', 'r')
str = fileopen.read()
print str
fileopen.close()
lines1 = sum(1 for line in open('C:/MPython/test.txt'))
wfile = open('C:/MPython/test.txt', 'a')
wfile.write("\n")
wfile.write(str(lines1).zfill(9))
wfile.close()



More information about the Python-list mailing list