Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-27-2004, 11:22 AM   #1
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Python and binary files

I need to save bytes, words, dwords, and floats to files using Python in binary format. Then I need to read those values in from the file in C.

Basically I need an "fwrite" for Python, and use "fread" for C. But only "basiaclly".


P.S. I already wrote functions "FileWriteByte", "FileWriteWord", etc... and they seem to work fine. My only real problem is with "FileWriteFloat". I don't know how to write a floating point number to a file, so I just do a "FileWriteDWord(FloatNum*1000, FileHandle)" and then divide by 1000 in C. So if someone could help me on this then I would be fine. I am just asking abou the others also because I think there might be a better way. Thanks!
Old 08-27-2004, 11:47 AM   #2
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
I'm by no means a Python expert, but I did something similar for a Python script to export models from Blender into my own custom binary format. Taking a look at my script, I used this code for my "BinaryFileWriter" class:

class BinaryFileWriter:
    def __init__(self, filename):
        self.__f = file(filename, "wb")
    def WriteByte(self, b):
        self.__f.write(struct.pack('<B', b))
    def WriteUint(self, i):
        self.__f.write(struct.pack('<I', int(i)))
    def WriteString(self, str, size):
        fmt = "<%is" % size
        self.__f.write(struct.pack(fmt, str))
    def WriteFloat(self, f):
        self.__f.write(struct.pack('<f', float(f)))
    def Close(self):
The struct.pack method being one that will get you binary output. I don't remember all the details of struct.pack, so you will probally want to check out the python docs for it as well.
Old 08-27-2004, 03:48 PM   #3
Registered: Aug 2002
Distribution: Debian
Posts: 540

Original Poster
Rep: Reputation: 32
Thumbs up

Hey thats cool. Thanks!

Btw, this is for a Blender script exporter! Thats cool that your a Blender user.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert a float to its binary or hex representation in Python? zero79 Programming 1 09-01-2005 11:19 AM
How to convert a float to its binary or hex representation in Python? zero79 Linux - Software 1 08-29-2005 10:30 PM
binary files in C sagar13 Programming 10 10-02-2004 02:29 PM
Binary files sodaforce Linux - Software 1 09-07-2003 06:31 PM
Using binary files KDE4me Linux - Newbie 1 04-24-2003 09:00 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:38 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration