0

I have a file I made with open. I need to write a float to it in binary format. How can I achieve this in Python?

Kröw
  • 504
  • 2
  • 13
  • 31
  • 1
    use `struct` module. please see https://stackoverflow.com/questions/16444726/binary-representation-of-float-in-python-bits-not-hex https://stackoverflow.com/questions/14431170/get-the-bits-of-a-float-in-python – jsofri Oct 20 '21 at 05:17
  • @Selcuk I meant `open`. Also, my original code is `open("file.xyz", "w")`. – Kröw Oct 20 '21 at 05:21
  • 1
    `open('file.xyz','wb').write( struct.pack('f',[3.7]) )` . You must use a binary file. – Tim Roberts Oct 20 '21 at 05:23
  • Thanks @TimRoberts. That seems to be what I was missing. – Kröw Oct 20 '21 at 05:25

0 Answers0