Created
November 9, 2023 20:12
-
-
Save abhishekkr/75119097f6e261e5519b407a2e9007de to your computer and use it in GitHub Desktop.
To toggle first two bytes of any file, quickly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Toggle the first & second bytes. | |
Syntax: python <py-file> filename | |
""" | |
import sys | |
def flip_first_bit(filename): | |
with open(filename, "r+b") as fp: | |
fp.seek(1, 0) | |
byte_one = fp.read(1) | |
byte_two = fp.read(1) | |
fp.seek(-2, 1) | |
fp.write(byte_two) | |
fp.write(byte_one) | |
if __name__ == "__main__": | |
filename = sys.argv[1] | |
flip_first_bit(filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment