0

I'm trying to write 32 bytes of binary data to a file but an extra byte is being added

wb mode doesn't seem to accept a newline argument so I'm not sure what to do here.

str_ = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'


with open('test.bin', 'wb') as f:
    f.write(str_)

You MUST view the file in a hex editor to be able to see the extra byte being added. Hex View of the file from VIM: https://i.stack.imgur.com/y8EpD.png

Bushfries
  • 85
  • 9
  • file has 32 bytes byt VIM shows 33 bytes. Maybe VIM add this value only on screen when it display it. What size has file before and after using VIM ? Maybe find different editor to check file - or write script which display data. – furas Aug 05 '19 at 19:59
  • test commentdddd – Bushfries Jan 06 '22 at 04:40
  • I can't reproduce your problem - when I display on Linux `od test.bin` (or `od -t x1 test.bin`) to see hex code then I see only 32 bytes without `0x0A`. And `f.write()` can't add new line. – furas Jan 07 '22 at 23:12
  • the same is with `xxd test.bin` - it also shows only 32 bytes without `0x0A`. – furas Jan 07 '22 at 23:20

1 Answers1

-1
os.system('touch efuse.bin')
with open('efuse.bin', 'wb') as f:
    f.write(generateBinString())

why you are writing newline=""

c=b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
with open('efuse.bin', 'wb') as f:
    f.write(c.rstrip())

with open('efuse.bin', 'rb') as f:
    print(f.read())
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

Line Feeds (0A) and Carriage Returns (0D)

enter image description here

CAN YOU SEE Python file.write creating extra carriage return

hope it helps

Jainil Patel
  • 1,284
  • 7
  • 16