6

I'm trying to write just one byte to a file in Python.

i = 10
fh.write( six.int2byte(i) )

Will output '0x00 0x0a'

fh.write( struct.pack('i', i) )

Will output '0x00 0x0a 0x00 0x00'

I want to write a single byte with the value 10 to the file.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
reza
  • 1,329
  • 2
  • 22
  • 37
  • 2
    How did you open `fh`? – RemcoGerlich Sep 07 '16 at 08:34
  • Are you talking about [file-objects](https://docs.python.org/3/glossary.html#term-file-object), [file-descriptors](https://docs.python.org/3/library/os.html#file-descriptor-operations) or actual [file handles on Windows](https://msdn.microsoft.com/en-us/library/windows/desktop/aa364225(v=vs.85).aspx)? – janbrohl Sep 07 '16 at 12:38
  • You should add code on how it was opened. DId you open with with regular `open` or something else? What is its type? – Ethan J Sep 06 '21 at 14:44

3 Answers3

14

You can just build a bytes object with that value:

with open('my_file', 'wb') as f:
    f.write(bytes([10]))

This works only in python3. If you replace bytes with bytearray it works in both python2 and 3.

Also: remember to open the file in binary mode to write bytes to it.

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
  • in python2.7 this generates: 0x31 0x5b 0x3d 0x30. with bytearray it generates 0x00 0x0a – reza Sep 07 '16 at 17:29
  • I take it back - I was using hexdump -x to look at the file which was padding the extra 0x00. The bytearray works. – reza Sep 07 '16 at 17:33
  • bytes/bytearray take values from 0 to 255 (unsigned) - so don't put in negative numbers – janbrohl Sep 07 '16 at 18:02
2

struct.pack("=b",i) (signed) and struct.pack("=B",i) (unsigned) pack an integer as a single byte which you can see in the docs for struct. ("=" is for using standard size and ignoring alignment - just in case) so you can do

import struct
i=10
with open('binfile', 'wb') as f:
    f.write(struct.pack("=B",i))
janbrohl
  • 2,626
  • 1
  • 17
  • 15
  • this also generates 0x00 0x0a (wither either =b or =B option) – reza Sep 07 '16 at 17:30
  • @reza - just tried this, on MSYS2 python2 and python3, and I get a file which is exactly 1 byte in size; with just 0x0a as content, when viewed either with `dhex` or with `hexdump -C binfile` – sdbbs Mar 08 '19 at 15:48
0
i=10
f=open('binfile', 'w', encoding='utf-8')
f.write(chr(i))
f.close()
Zibri
  • 9,096
  • 3
  • 52
  • 44
  • 1
    While this code may answer the question, providing additional context regarding *how* and/or *why* it solves the problem would improve the answer's long-term value. This is particularly important when answering old questions (this one is over five years old) with existing and accepted answers. – Sven Eberth Sep 06 '21 at 19:55
  • Furthermore it must be `f.close()`, but in general the use as context manager is recommended anyway. – Sven Eberth Sep 06 '21 at 19:57
  • ok smarty, I forgot the ( ). You really make people welcome. I don't care if it's accepted old or what. I just posted another (and simpler) way to do it. Oh and you didn't even notice I forgot to specify the encoding, which I now added. :P – Zibri Sep 08 '21 at 04:30