Skip to content
Advertisement

Subtract 2 hex files line by line in python

Have 2 files with hex values

file1.txt

JavaScript

file2.txt

JavaScript

using python i am trying to subtract line 1 of file1.txt from line 1 of file2.txt and so on. My codes

JavaScript

but I am only able to get first line subtract value, not getting all 3 lines value.

Advertisement

Answer

The problem is with: line1 = f1.readline().strip() and line2 = f2.readline().strip(). Since you are reading the first line only from each files, you have a single result. You need to do it for all the lines, not just the first one.

Read all the lines, and strip each of them:

JavaScript

Now, modify the add function signature:

JavaScript

The key1 and key2 functions are fine, so they don’t need to be modified.

Now, call add for each pair of lines from the files, iterating them parallely with zip:

JavaScript

OUTPUT:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement