Reading binary files

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 11
Reputation: Miyuki is an unknown quantity at this point 
Solved Threads: 0
Miyuki Miyuki is offline Offline
Newbie Poster

Reading binary files

 
0
  #1
Mar 27th, 2008
I want to use Python to make something similar to the fc command found in MS-DOS and Windows. How would I handle opening 2+ files and reading them a byte at a time for comparing?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 311
Reputation: BearofNH is on a distinguished road 
Solved Threads: 40
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz

Re: Reading binary files

 
0
  #2
Mar 27th, 2008
You could start with this and maybe clean it up a bit.
  1. import struct
  2. import sys
  3.  
  4. blocksize = 0x2000
  5. errstart = 0 # File offset of start of error span
  6. errend = 0 # File offset just past end of error span
  7. diffct = 0 # Difference region count
  8.  
  9. chunk_char = {8:'Q'}
  10.  
  11. def endSpan(loc):
  12. global diffct,errstart,errend,inSpan
  13. errend = loc
  14. elength = loc-errstart
  15. print "Span of %10X bytes bad from %12X to %12X\n" % (elength, errstart, errend-1)
  16. diffct += 1
  17. return
  18.  
  19. def unpack(buffer, chunk):
  20. rv = []
  21. for x in range(0, len(buffer)/chunk):
  22. rv.append('%X' % struct.unpack(chunk_char[chunk],buffer[chunk*x:chunk*(x+1)]))
  23. return rv
  24.  
  25. def usage():
  26. print "Usage: %s file1 file2" % sys.argv[0]
  27. return
  28.  
  29. def main():
  30. global errstart, inSpan
  31. argc = len(sys.argv)
  32. if argc < 3:
  33. usage()
  34. return 1
  35.  
  36. f1name = sys.argv[1]
  37. f1d = open(f1name, 'rb')
  38. if f1d==None:
  39. print "Error opening %s for read", f1name
  40.  
  41. f2name = sys.argv[2]
  42. f2d = open(f2name, 'rb')
  43. if f2d==None:
  44. print "Error opening %s for read", f2name
  45.  
  46. if f1d==None or f2d==None: return 1 # Exit if can't open file
  47.  
  48. print "Comparing %s and %s" % (f1name, f2name)
  49.  
  50. diffs = 0 # Count of difference regions
  51. f1off = 0 # Offset of read
  52. f2off = 0 # ...
  53. stride = 8 # Our unit of work
  54. maxlpe = 30 # Max lines to print per error
  55.  
  56. inSpan = False # Not in a span of errors
  57. s1 = s2 = "Something not null" # Just not None, dammit!
  58.  
  59. while True:
  60. if s1 != None:
  61. s1 = f1d.read(blocksize)
  62.  
  63. if s2 != None:
  64. s2 = f2d.read(blocksize)
  65.  
  66. if s1=="" or s2=="":
  67. if inSpan:
  68. endSpan(f1off)
  69. break
  70.  
  71. if s1!=s2:
  72. lst1 = unpack(s1,stride)
  73. lst2 = unpack(s2,stride)
  74. lst = [(long(lst1[x],16),long(lst2[x],16),f1off+stride*x) for x in range(len(lst1))]
  75.  
  76. for e in lst:
  77. if e[0]==e[1]: # Equal?
  78. if inSpan:
  79. endSpan(e[2])
  80. inSpan = False
  81. else: # Not equal
  82. if not inSpan:
  83. inSpan = True
  84. errstart = e[2]
  85. curlpe = 0 # Lines printed for this error
  86. print " FileOffset 1stFileContents 2ndFileContents XOR"
  87. if curlpe < maxlpe: # OK to print this error line?
  88. print "%12X %16X %16X %16X" % (e[2], e[0], e[1], e[0]^e[1])
  89. curlpe += 1
  90. elif curlpe == maxlpe:
  91. print " ... and so on ..."
  92. curlpe += 1
  93.  
  94. # Here when done with these bits
  95.  
  96. f1off += blocksize
  97. f2off += blocksize
  98.  
  99. f1d.close()
  100. f2d.close()
  101. print "%u difference regions found" % diffct
  102. main()
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Reading binary files

 
0
  #3
Mar 27th, 2008
You might want to play with the Python builtin module filecmp:
  1. # compare two files and check if they are equal
  2. # files can be binary or text based
  3.  
  4. import filecmp
  5.  
  6. # pick two files you want to compare ...
  7. file1 = "Boing1.wav"
  8. file2 = "Boing2.wav"
  9.  
  10. if filecmp.cmp(file1, file2):
  11. print "Files %s and %s are identical" % (file1, file2)
  12. else:
  13. print "Files %s and %s differ!" % (file1, file2)
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC