Python:以十六进制查看所有文件

发布于 2024-09-02 20:39:15 字数 1119 浏览 2 评论 0原文

我正在编写一个 python 脚本,它查看常见的计算机文件并检查它们是否有相似的字节、单词、双字。虽然我需要/想要查看十六进制的文件,但 ande 似乎无法真正让 python 在 python 中打开一个简单的文件。我尝试过使用十六进制作为编码的 codecs.open ,但是当我对文件描述符进行操作时,它总是会返回,

      File "main.py", line 41, in <module>
    main()
  File "main.py", line 38, in main
    process_file(sys.argv[1])
  File "main.py", line 27, in process_file
    seeker(line.rstrip("\n"))
  File "main.py", line 15, in seeker
    for unit in f.read(2):
  File "/usr/lib/python2.6/codecs.py", line 666, in read
    return self.reader.read(size)
  File "/usr/lib/python2.6/codecs.py", line 472, in read
    newchars, decodedbytes = self.decode(data, self.errors)
  File "/usr/lib/python2.6/encodings/hex_codec.py", line 50, in decode
    return hex_decode(input,errors)
  File "/usr/lib/python2.6/encodings/hex_codec.py", line 42, in hex_decode
    output = binascii.a2b_hex(input)
TypeError: Non-hexadecimal digit found





def seeker(_file):
 f = codecs.open(_file, "rb", "hex")
 for LINE in f.read():
      print LINE
 f.close()

我真的只是想查看文件,并对它们进行操作,就像在 xxd 这样的十六进制编辑器中一样。还可以一次以一个字的增量读取文件。

不,这不是作业。

I am writing a python script which looks at common computer files and examines them for similar bytes, words, double word's. Though I need/want to see the files in Hex, ande cannot really seem to get python to open a simple file in python. I have tried codecs.open with hex as the encoding, but when I operate on the file descriptor it always spits back

      File "main.py", line 41, in <module>
    main()
  File "main.py", line 38, in main
    process_file(sys.argv[1])
  File "main.py", line 27, in process_file
    seeker(line.rstrip("\n"))
  File "main.py", line 15, in seeker
    for unit in f.read(2):
  File "/usr/lib/python2.6/codecs.py", line 666, in read
    return self.reader.read(size)
  File "/usr/lib/python2.6/codecs.py", line 472, in read
    newchars, decodedbytes = self.decode(data, self.errors)
  File "/usr/lib/python2.6/encodings/hex_codec.py", line 50, in decode
    return hex_decode(input,errors)
  File "/usr/lib/python2.6/encodings/hex_codec.py", line 42, in hex_decode
    output = binascii.a2b_hex(input)
TypeError: Non-hexadecimal digit found





def seeker(_file):
 f = codecs.open(_file, "rb", "hex")
 for LINE in f.read():
      print LINE
 f.close()

I really just want to see files, and operate on them as if it was in a hex editor like xxd. Also is it possible to read a file in increments of maybe a word at a time.

No this is not homework.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

殊姿 2024-09-09 20:39:15

codecs.open(_file, "rb", "hex") 正在尝试将文件的内容解码为十六进制,这就是它失败的原因。

考虑到您的其他“一次单词”目标(我假设您的意思是“计算机单词”,即 32 位?),您最好将打开的文件封装到您自己的类中。例如:

class HexFile(object):
    def __init__(self, fp, wordsize=4):
        self.fp = fp
        self.ws = wordsize
    def __iter__(self):
        while True:
            data = self.fp.read(self.ws)
            if not data: break
            yield data.encode('hex')

当然,加上您认为有帮助的任何其他实用方法。

codecs.open(_file, "rb", "hex") is trying to decode the file's contents as being hex, which is why it's failing on you.

Considering your other "word at a time" target (I assume you mean "computer word", i.e. 32 bits?), you'll be better off encapsulating the open file into a class of your own. E.g.:

class HexFile(object):
    def __init__(self, fp, wordsize=4):
        self.fp = fp
        self.ws = wordsize
    def __iter__(self):
        while True:
            data = self.fp.read(self.ws)
            if not data: break
            yield data.encode('hex')

plus whatever other utility methods you'd find helpful, of course.

醉城メ夜风 2024-09-09 20:39:15

您可以通过将整数参数传递给 read 来读取设定数量的字节:

32bits = file.read(4)

您可以使用 seek 查找文件中的某个位置:

file.seek(100) # Seeks to byte 100

You can read a set number of bytes by passing an integer argument to read:

32bits = file.read(4)

You can seek to a position in the file using seek:

file.seek(100) # Seeks to byte 100
凉栀 2024-09-09 20:39:15

如果这会更清楚......:
def hexfile(文件路径):
fp=打开(文件路径)
而真实:
数据 = fp.read(4)
如果没有数据:中断
print data.encode('hex')

file_path 类似于“C:/somedir/filename.ext”
顺便说一句,这是一个很好的方法,它对我来说会很有效。 :)

if this will be more clear... :
def hexfile(file_path):
fp=open(file_path)
while True:
data = fp.read(4)
if not data: break
print data.encode('hex')

file_path is something like "C:/somedir/filename.ext"
it nice method btw it will work nicely for me. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文