Not sure what I am doing wrong here. The main function works fine, but appending/writing the data into the .txt file function is not working. I keep getting "NameError: name 'data' is not defined". I'm guessing it's a scope problem?
Question: How do I run the main function and then write the output to a file? How can I access the output of the main function so I can run other functions on it?
Please help and Thank you!!
import sys, os
print("\n-------------------------- String HexDump ------------------------------------\n")
def main():
try:
with open(sys.argv[1], 'rb') as file:
for line in range(0, os.path.getsize(sys.argv[1]), 60):
data = file.read(60)
data = str(data)
print(data)
except:
print('Usage: {} <filename>'.format(os.path.basename(sys.argv[0])))
str = lambda data: ''.join(31 < i < 127 and chr(i) or '.' for i in data)
if __name__ == '__main__':
main()
def HexStrFileDump():
with open('HEXDUMPFILE2.txt','wb') as HexFile:
HexFile.write(data)
HexStrFileDump()