0

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()
Dmurphy
  • 33
  • 7

2 Answers2

0

You are calling all the functions in the incorrect order and with variables that do not exist (data) and out of scope.

If you use main() then concentrate all your code here and in subfunctions, best practice is to not spread around.

Your error comes from the improper use of HexFile.write(data) : data is the name of your lambda, not where your results are.

Here is a proper file hexdumper I wrote in 5 minutes for this answer:

import sys

def main():
    print("\n-------------------------- String HexDump ------------------------------------\n")
    try:
        with open(sys.argv[1], 'rb') as file:
            i = 0; clear = ""
            for char in file.read():
                if char > 0x19 and char < 0x7f:
                    clear = clear + chr(char)
                else:
                    clear = clear + "?"
                if i % 4 == 0 and i != 0:
                    clear = clear + " "
                    sys.stdout.write(" ")
                if i % 16 == 0 and i != 0:
                    sys.stdout.write(clear+"\n")
                    clear = ""
                    i = 0
                sys.stdout.write("%02X" % char)
                i += 1
            if i % 16 != 0:
                sys.stdout.write(" "+clear+"\n")
    except:
        raise
        print('Usage: {} <filename>'.format(os.path.basename(sys.argv[0])))


if __name__ == '__main__':
    main()
Fabien
  • 4,862
  • 2
  • 19
  • 33
0

You really should have posted the stack trace so we could see where the error is coming from, but you're lucky in that there is exactly one place in this program that can cause the error you're describing.

def HexStrFileDump():
    with open('HEXDUMPFILE2.txt','wb') as HexFile:
        HexFile.write(data)
                      ^

At that point in the code, the variable data is not defined. It would have to have some value assigned to it either earlier in the function, or at the module level, but you have not done that.

You have a data variable in main(), but that variable only exists inside main(), and you also have a different data variable inside the lambda function which you call str, but again, that variable only exists inside that function. As far as the code in HexStrFileDump() is concerned, those other data variables do not exist, which is why it's complaining about data not being defined.

David Z
  • 128,184
  • 27
  • 255
  • 279
  • Ok, so here is where I am at. I added "global data" and "return data" in my main function. Now when I run it, it does write to the file, however, it only writes the first line of data. I haven't been able to write everything to the file. To me, it seems I now have access to the data from the global variable added, or am I still wrong? Forgive me I am still learning. @David Z – Dmurphy Jul 15 '17 at 17:49