Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6114025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:55:36+00:00 2026-05-23T14:55:36+00:00

When writing binary files in Python I seem to be missing some bytes. I’ve

  • 0

When writing binary files in Python I seem to be missing some bytes. I’ve tried this with the “write” function and with the “array.tofile” function. Here is some example code:

import zlib, sys, os, array
from struct import unpack
from array import array


inputFile = 'strings.exe'

print "Reading data from: ", inputFile

print 'Input File Size:', os.path.getsize(inputFile)

f = open(inputFile, 'rb')
#compressedDocument = 

document = f.read()
documentArray = array('c', document)
print 'Document Size:', len(documentArray)

copyFile = open( 'Copy of ' + inputFile, 'wb')
documentArray.tofile(copyFile)
#copyFile.write(document)
copyFile.close


print 'Output File Size:', os.path.getsize('Copy of ' + inputFile)

print 'Missing Bytes:', os.path.getsize(inputFile) - os.path.getsize('Copy of ' + inputFile)
f.close()

Gives the following output:

Reading data from:  strings.exe
Input File Size: 136592
Document Size: 136592
Output File Size: 135168
Missing Bytes: 1424

I don’t understand why those bytes aren’t being written. I’ve tried this on multiple files with a varying number of missing bytes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:55:36+00:00Added an answer on May 23, 2026 at 2:55 pm

    If you actually try to compare the two binary files (if you are under unix you use the cmp command) you will see the two files are identical.

    EDIT: As correctly pointed out by John in his answer, the difference in byte size is due to not closing the file before measuring its length. The correct line in the code should be copyFile.close() [invoking the method] instead of copyFile.close [which is the method object].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've looked at binary reading and writing objects in c++ but are having some
I'm writing an iterative function to search a binary tree for a certain value.
I'm writing a class interface that needs to return references to binary files. Typically
I'm trying to write strictly binary data to files (no encoding). The problem is,
How should a program read and write float numbers from and into binary files
I am writing a parser for delphi's dfm's files. The lexer looks like this:
I'm planning on writing some code for encrypting files in javascript locally. For large
I'm interested in writing a short python script which uploads a short binary file
I am writing a php program to write a binary file (may be video
I am writing to binary file using fstream and when open the file using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.