Help to convert Number to String

V

Vamsi

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()

Thanks.
 
S

Stefan Schwarzer

Hi Vamsi,

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?

Which Python version do you use?

For which statement exactly do you get the message?
here is the code

lines1 = sum(1 for line in open('C:/test1.txt'))
wfile = open('C:/test1.txt'', 'a')

You have two quotes here after the filename. These give a
SyntaxError here. Python sees two concatenated strings,
"C:/test1.txt" and ", " and stumbles over the a immediately
after the closing quote of the second string.
wfile.write(str(lines1).zfill(9))
wfile.close()

If I remove the redundant quote character and substitute
filenames appropriate for my system, everything works.

Maybe the code you included in this post isn't the same
which led to the error?

Stefan
 
S

Steven D'Aprano

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:

and the file is correctly updated:
'hello\nworld\n000000002'
 
V

Vamsi

Hi Vamsi,



Which Python version do you use?

For which statement exactly do you get the message?



You have two quotes here after the filename. These give a
SyntaxError here. Python sees two concatenated strings,
"C:/test1.txt" and ", " and stumbles over the a immediately
after the closing quote of the second string.


If I remove the redundant quote character and substitute
filenames appropriate for my system, everything works.

Maybe the code you included in this post isn't the same
which led to the error?

Stefan

Thank you Stefan,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()
 
V

Vamsi

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:




and the file is correctly updated:


'hello\nworld\n000000002'

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()
 
D

Dennis Lee Bieber

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()

Besides the aforementioned rebinding of str to a string instead of
module... I'd probably have coded this something more like [UNTESTED]:

import os

theFile = open("C:/MPython/Test.txt", "r+")
for i, ln in enumerate(theFile):
lineCount = i #this is just in case some future version of
#Python suddenly deletes loop variables
#on exit; otherwise just do pass
# and change lineCount to i below
theFile.seek(0, os.SEEK_END) #need a seek to switch read to write
theFile.write("\n%09i\n" % lineCount + 1) #since count starts at 0
theFile.close()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top