16

I am running into the following error while writing the value into a file. Can you please help me figure out what is the issue here and how to fix it?

row = 649
with open(r'\\loc\dev\Build_ver\build_ver.txt','r+') as f:
    f.write(row)
print row

Error:

Traceback (most recent call last):
  File "latest_rev.py", line 6, in <module>
    f.write(row)
TypeError: expected a character buffer object
Lerner Zhang
  • 6,184
  • 2
  • 49
  • 66
  • possible duplicate of [TypeError: expected a character buffer object - while trying to save integer to textfile](http://stackoverflow.com/questions/9786941/typeerror-expected-a-character-buffer-object-while-trying-to-save-integer-to) – Florian Brucker May 06 '15 at 20:41

3 Answers3

40

Assuming you just want to write the string '649' to the file, change row to '649' or issue f.write(str(row)).

timgeb
  • 76,762
  • 20
  • 123
  • 145
2

You can do what timgeb did or you can do

row = str(649)
bruh
  • 41
  • 5
1

I had the same error, in my code:

s.translate(table)

The s obj was string. The issue was s.translate was expecting a unicode string. So, the fix was to use:

unicode(s).translate(table)
timgeb
  • 76,762
  • 20
  • 123
  • 145
Roozbeh Zabihollahi
  • 7,207
  • 45
  • 39