Nick A Nick A - 6 months ago 16
Python Question

File not opening past NUL byte

I'm having trouble seeing the contents of an entire file due to there being NUL bytes throughout the file.

The following is a hexdump of my file(it's in a code segment to maintain readability):

0000-0010: 22 fc 6b 0c-6c 3f 6d 0c-a2 ea da b6-dc d6 6e 00 ".k.l?m. ......n.
0000-0020: 22 d4 66 03-68 02 60 60-f0 15 f0 07-30 00 12 1a ".f.h.`` ....0...
0000-0030: c7 17 77 08-69 ff a2 f0-d6 71 a2 ea-da b6 dc d6 ..w.i... .q......
0000-0040: 60 01 e0 a1-7b fe 60 04-e0 a1 7b 02-60 1f 8b 02 `...{.`. ..{.`...
0000-0050: da b6 60 0c-e0 a1 7d fe-60 0d e0 a1-7d 02 60 1f ..`...}. `...}.`.
0000-0060: 8d 02 dc d6-a2 f0 d6 71-86 84 87 94-60 3f 86 02 .......q ....`?..
0000-0070: 61 1f 87 12-46 00 12 78-46 3f 12 82-47 1f 69 ff a...F..x F?..G.i.
0000-0080: 47 00 69 01-d6 71 12 2a-68 02 63 01-80 70 80 b5 G.i..q.* h.c..p..
0000-0090: 12 8a 68 fe-63 0a 80 70-80 d5 3f 01-12 a2 61 02 ..h.c..p ..?...a.
0000-00a0: 80 15 3f 01-12 ba 80 15-3f 01 12 c8-80 15 3f 01 ..?..... ?.....?.
0000-00b0: 12 c2 60 20-f0 18 22 d4-8e 34 22 d4-66 3e 33 01 ..`...". .4".f>3.
0000-00c0: 66 03 68 fe-33 01 68 02-12 16 79 ff-49 fe 69 ff f.h.3.h. ..y.I.i.
0000-00d0: 12 c8 79 01-49 02 69 01-60 04 f0 18-76 01 46 40 ..y.I.i. `...v.F@
0000-00e0: 76 fe 12 6c-a2 f2 fe 33-f2 65 f1 29-64 14 65 02 v..l...3 .e.)d.e.
0000-00f0: d4 55 74 15-f2 29 d4 55-00 ee 80 80-80 80 80 80 .Ut..).U ........
0000-0100: 80 00 00 00-00 00 c0 c0-c0 00 ff 00-6b 20 6c 00 ........ ....k.l.
0000-0110: a2 f6 db c4-7c 04 3c 20-13 02 6a 00-6b 00 6c 1f ....|.<. ..j.k.l.
0000-0120: a2 fa da b1-da c1 7a 08-3a 40 13 12-a2 f6 6a 00 ......z. :@....j.
0000-0126: 6b 20 db a1-00 ee k.....


but instead of getting:

"ük l?m ¢êÚ¶ÜÖn "Ôfh``ðð0 Çwiÿ¢ðÖq¢êÚ¶ÜÖ`à¡{þ`à¡{`‹Ú¶` à¡}þ`
à¡}`ÜÖ¢ðÖq†„‡”`?†a‡F xF?‚GiÿG iÖq*hc€p€µŠhþc
€p€Õ?¢a€?º€?È€?Â` ð"ÔŽ4"Ôf>3fhþ3hyÿIþiÿÈyIi`ðvF@vþl¢òþ3òeñ)deÔUtò)ÔU €€€€€ ÀÀÀ ÿ k l ¢öÛÄ|< j k l¢úÚ±ÚÁz:@¢öj k Û¡ î


when printing as I would expect, I'm getting:

"ük l?m ¢êÚ¶ÜÖn


My current attempt is:

def loadProgram(f):
fin = open(f,'rb')
buf = fin.read()


and I can't think of any way to move further.

Is the entire file not reading or is there a problem with the printing?

Answer

There is a problem with windows, that nul-bytes terminates any text-string. So Windows stops printing at the first \0. You can use repr to print escape characters.