0

I am creating this "hexdump" program, and the problem that I am having is writing in the file because I need it to dump the data in a file instead of keeping it in the terminal.

P.S: I'm a bit new to Python, so my friend helped me in coding this.

Here is the code:

import sys
import pickle


def hexdump(fname, start, end, width):
    for line in get_lines(fname, int(start), int(end), int(width)):
    nums = ["%02x" % ord(c) for c in line]
    txt = [fixchar(c) for c in line]       
    x = " ".join(nums), "".join(txt)
    y = ' '.join(x)
    print (y)
    f = open('dump.txt', 'w')
    pickle.dump(y, f)
    f.close()


def fixchar(char):    
    from string import printable
    if char not in printable[:-5]:
        return "."
    return char


def get_lines(fname, start, end, width):
    f = open(fname, "rb")
    f.seek(start)
    chunk = f.read(end-start)
    gap = width - (len(chunk) % width)
    chunk += gap * '\000'
    while chunk:
         yield chunk[:width]
         chunk = chunk[width:]


if __name__ == '__main__':
    try:
        hexdump(*sys.argv[1:5])
    except TypeError:
        hexdump("hexdump.py", 0, 100, 16)

I know that it is quite a mess, but I need to print the data in a file.

Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57
recherpoi
  • 11
  • 3

2 Answers2

2

For appending to a file, you need to use 'a' mode rather than 'w' (which overwrites), like so:

f = open('dump.txt', 'a')

But, in case of pickling, (saving objects into files), you probably want to modify your code according to this answer: https://stackoverflow.com/a/12762056/1256112

def hexdump(fname, start, end, width):
    with open('dump.txt', 'ab') as writeable:
        for line in get_lines(fname, int(start), int(end), int(width)):
            nums = ["%02x" % ord(c) for c in line]
            txt = [fixchar(c) for c in line]       
            x = " ".join(nums), "".join(txt)
            y = ' '.join(x)
            print(y)
            pickle.dump(y, writeable)
Community
  • 1
  • 1
Vlad M
  • 477
  • 3
  • 10
  • Thanks it worked, but i want the output format to be like this : 69 6d 70 6f 72 74 20 73 79 73 0d 0a 69 6d 70 6f (Ascii here) – recherpoi Dec 25 '15 at 11:21
  • 1
    It worked perfectly thanks, but at each end of every line it writes "p0" and every line starts with ' .S" ' any ideas ? – recherpoi Dec 25 '15 at 11:48
1

Using this code it worked perfectly:

def hexdump(fname, start, end, width):
    with open('dump.txt', 'ab') as writeable:
        for line in get_lines(fname, int(start), int(end), int(width)):
            nums = ["%02x" % ord(c) for c in line]
            txt = [fixchar(c) for c in line]       
            x = " ".join(nums), "".join(txt)
            y = ' '.join(x)
            print(y)
            pickle.dump(y, writeable)

Now the problem is that when I open dump.txt I find this:

S'69 6d 70 6f 72 74 20 73 79 73 0d 0a 69 6d 70 6f import sys..impo'
p0
.S'72 74 20 70 69 63 6b 6c 65 0d 0a 0d 0a 0d 0a 64 rt pickle......d'
p0

so how to get rid of "S'" and "p0"?

Delimitry
  • 2,987
  • 4
  • 30
  • 39
recherpoi
  • 11
  • 3