Python:以十六进制查看所有文件
我正在编写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
codecs.open(_file, "rb", "hex")
正在尝试将文件的内容解码为十六进制,这就是它失败的原因。考虑到您的其他“一次单词”目标(我假设您的意思是“计算机单词”,即 32 位?),您最好将打开的文件封装到您自己的类中。例如:
当然,加上您认为有帮助的任何其他实用方法。
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.:
plus whatever other utility methods you'd find helpful, of course.
您可以通过将整数参数传递给
read
来读取设定数量的字节:您可以使用
seek
查找文件中的某个位置:You can read a set number of bytes by passing an integer argument to
read
:You can seek to a position in the file using
seek
:如果这会更清楚......:
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. :)