How to check if two .pyc files are the same byte code

Last updated on May 31, 2020 by Dan Nanni

Question: I have two .pyc Python bytecode files that I obtained from somewhere. Is there an easy way to check if these two .pyc files contain the same byte code?

Since .pyc files are in binary format, you may attempt to either compare their md5sum outputs, or use cmp command to do binary diff. However, using these tools will not work for .pyc files as is, because .pyc files contain extra metadata in them, which may be different even when the byte code itself is identical.

More specifically, the first 4 bytes of a .pyc file store a "magic" number which identifies the version of Python used to compile .pyc file. The next 4 bytes contain the timestamp of the Python source file.

Therefore, what you can do is to ignore the first 8 bytes of .pyc files, and compare the rest of the files. This can be achieved by the following command.

$ cmp <(tail -c +8 file.pyc)  <(tail -c +8 file2.pyc)

Note that this command works for bash shell.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean