I"m trying to make a very straightforward "counter" that is expected to store track of how many type of times my regimen has actually been executed.

You are watching: Typeerror: expected a string or other character buffer object

First, I have actually a textfile that only includes one character: 0

Then I open the file, parse it as an int, add 1 to the value, and also then try to rerotate it to the textfile:

f = open("testfile.txt", "r+")x = f.read()y = int(x) + 1print(y)f.write(y)f.close()I"d like to have y overcompose the worth in the textfile, and also then cshed it. But all I gain is TypeError: intended a character buffer object.

Edit:

Trying to parse y as a string:

f.write(str(y))gives

IOError: Error
python file parsing
Share
Improve this question
Follow
edited Aug 1 "14 at 15:38

*

Martin Thoma
96.2k122122 gold badges518518 silver badges784784 bronze badges
asked Mar 20 "12 at 12:40
*

BSGBSG
1,31233 gold badges1313 silver badges2525 bronze badges
Add a comment |

3 Answers 3


Active Oldest Votes
77
Have you checked the docstring of write()? It says:

write(str) -> None. Write string str to file.

Note that due to buffering, flush() or close() might be necessary prior to the file on disk reflects the data composed.

So you must transform y to str initially.

Also note that the string will certainly be composed at the present position which will certainly be at the finish of the file, because you"ll already have actually check out the old value. Use f.seek(0) to gain to the start of the file.`

Edit: As for the IOError, this problem seems connected. A cite from there:

For the settings wbelow both review and creating (or appending) are permitted (those which encompass a "+" sign), the stream should be fluburned (fflush) or repositioned (fseek, fsetpos, rewind) between either a reading operation followed by a composing procedure or a writing procedure complied with by a analysis procedure.

So, I indicate you try f.seek(0) and also possibly the problem goes ameans.


Share
Improve this answer
Follow
edited Mar 20 "12 at 12:50
answered Mar 20 "12 at 12:45
*

Lev LevitskyLev Levitskies
56.9k1818 gold badges132132 silver badges157157 bronze badges
0
Add a comment |
9
from __future__ import with_statementwith open("file.txt","r+") as f: respond to = str(int(f.read().strip())+1) f.seek(0) f.write(counter)
Share
Improve this answer
Follow
answered Mar 20 "12 at 12:54
*

Burhan KhalidBurhan Khalid
155k1717 gold badges220220 silver badges259259 bronze badges
0
Add a comment |
0
Just try the code below:

As I view you have placed "r+" or this command open the file in check out mode so you are not able to compose right into it, so you have to open up file in create mode "w" if you want to overcompose the file contents and write new information, otherwise you deserve to appfinish information to file by making use of "a"

I hope this will certainly assist ;)

f = open("testfile.txt", "w")# just put "w" if you desire to write to the file x = f.readlines() #this command also will certainly check out file linesy = int(x)+1print yz = str(y) #making data as string to stop buffer errorf.write(z)f.close()
Share
Improve this answer
Follow
answered May 17 "14 at 8:30

*

salah Laaroussisalah Laaroussi
15511 gold badge22 silver badges55 bronze badges
0
Add a comment |

Your Answer


Thanks for contributing a solution to Stack Overflow!

Please be sure to answer the question. Provide details and share your research!

But avoid

Asking for assist, clarification, or responding to other answers.Making statements based upon opinion; back them up through recommendations or personal suffer.

To learn more, check out our tips on writing great answers.

See more: Faux Locs: How To Make Faux Locs Look Natural Faux Locs, How To Install The Most Realistic Faux Locs


Draft saved
Draft discarded

Sign up or log in


Sign up utilizing Google
Sign up utilizing Facebook
Sign up making use of Email and also Password
Submit

Blog post as a guest


Name
Email Required, but never shown


Blog post as a guest


Name
Email

Required, yet never before shown


Message Your Answer Discard

By clicking “Blog post Your Answer”, you agree to our regards to service, privacy policy and cookie policy


Not the answer you're looking for? Browse other concerns tagged python file parsing or ask your own question.


The Overcirculation Blog
Featured on Meta
Linked
-1
How to create HTML to a file dynamically in Python
16
TypeError: supposed a character buffer object
9
TypeError: meant a character buffer object
3
Python Pretty Table output to text file
0
CSV'ify a variable
0
Python - transform pdf to text, encoding error
0
python: updating file by finding distinct values in between two files, and also then appfinishing additionally information to bottom
0
TypeError: supposed a character buffer object
0
applying function to all columns in an numpy array
1
Parsing word papers right into excel file through single word per row
See even more connected questions
Related
2
TypeError: expected a character buffer object
5
python open('file','r+') giving weird outcome
9
TypeError: meant a character buffer object
1
TypeError: expected a character buffer object while translating
0
TypeError: expected a character buffer object
16
TypeError: expected a character buffer object
2
TypeError: expected a character buffer object in Python
Hot Netoccupational Questions more hot questions

Inquiry feed
Subscribe to RSS
Question feed To subscribe to this RSS feed, copy and also paste this URL right into your RSS reader.


lang-py
Stack Overcirculation
Products
Company type of
Stack Exadjust Network-related
website architecture / logo design © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev2021.9.14.40215


Stack Overcirculation functions finest with JavaScript allowed
*

Your privacy

By clicking “Accept all cookies”, you agree Stack Exadjust deserve to keep cookies on your gadget and discshed indevelopment in accordance through our Cookie Policy.