Filename = raw_input ('enter file name: ') <br/> F = open (filename, 'rb') <br/> F. seek (0, 0) <br/> Index = 0 <br/> for I in range (0, 16): <br/> Print "% 3 S" % hex (I ), <br/> Print <br/> for I in range (0, 16): <br/> Print "%-3 s" % "#", <br/> Print <br/> while true: <br/> temp = f. read (1) <br/> If Len (temp) = 0: <br/> Break <br/> else: <br/> Print "% 3 S" % TEMP. encode ('hex'), <br/> Index = index + 1 <br/> If Index = 16: <br/> Index = 0 <br/> Print <br/> F. close ()
The following figure shows the effect of reading a BMP image.
From this, we can see that the print statement and C's printf have the same format requirements, or Python adopts the C format specification.
Print "%-3 s" % "#",
A comma prevents line breaks from being automatically generated.-3 indicates that the display occupies 3 Characters and is displayed from the left (from the right by default ).
F. Read (1)
Read one byte each time. If the read length is 0, it is at the end of the file.
There are many special things about the python syntax, so you need to learn it later.