blockSize = 100 fileList = ["filepath1", "filepath2", "filepath3"] def invert(x): return ~x & 0xFF def repair(filePath, blockSize): try: with open(filePath,"rb+") as f: block = f.read(blockSize) newStr = '' for ch in block: uc = ord(ch) inv = invert(uc) newStr += chr(inv) f.seek(0) f.write(newStr) f.close() except Exception, e: raise e print "Successful!" for f in fileList: try: repair(f, blockSize) except Exception, e: print e