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

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 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.

Subscribe to Ask Xmodulo

Do you want to receive Linux related questions & answers published at Ask Xmodulo? Enter your email address below, and we will deliver our Linux Q&A straight to your email box, for free. Delivery powered by Google Feedburner.

One thought on “How to check if two .pyc files are the same byte code

  1. Nice. I have something in a pyc and no idea if its the older version or not(of course its my code but still confusing). This may help telling the two apart.

Leave a comment

Your email address will not be published. Required fields are marked *

Current day month ye@r *