.Thumbdata3文件提取。 TypeError:需要类似字节的对象,而不是'str'

问题描述 投票:0回答:1

我知道有类似的线程,我已经通过它们,但他们没有帮助我的情况:

前段时间我保存了两个大小约为500mb的.thumbdata3文件。 This stackexchange thread声称我可以使用python脚本从文件中提取小jpeg:

#!/usr/bin/python

"""extract files from Android thumbdata3 file"""

f=open('thumbdata3.dat','rb')
tdata = f.read()
f.close()

ss = '\xff\xd8'
se = '\xff\xd9'

count = 0
start = 0
while True:
    x1 = tdata.find(ss,start)
    if x1 < 0:
        break
    x2 = tdata.find(se,x1)
    jpg = tdata[x1:x2+1]
    count += 1
    fname = 'extracted%d03.jpg' % (count)
    fw = open(fname,'wb')
    fw.write(jpg)
    fw.close()
    start = x2+2

但是它返回了这个错误:

Traceback (most recent call last):
  File "... extract.py", line 15, in <module>
    x1 = tdata.find(ss,start)
TypeError: a bytes-like object is required, not 'str'

在搜索之后我认为错误可能是在使用2.7和3.5方法之间,并且将f.open函数中的'rb'更改为'r'只是为了得到这个错误:

Traceback (most recent call last):
  File "...\Thumbdata\thumbadata extract.py", line 6, in <module>
    tdata = f.read()
  File "...\Anaconda3\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 277960004: character maps to <undefined>

值得一提的是,脚本和文件都在同一个文件夹中。我正在使用Atom和Python运行包,以及Anaconda3。

任何帮助表示赞赏。

python file extraction
1个回答
1
投票

你必须在f = open('thumbdata3.dat','rb')中继续使用rb模式读取二进制文件来读取二进制数据。

问题是f是二进制流,然后find函数需要一个字节类型的参数,这在Python3中是新的。

ss和se被指定为字符串值,因此它的类型是字符串(我猜ss和se代表字符串start和字符串结尾)。

您需要使用encode()函数将这些字符串编码为二进制类型:

x1 = tdata.find(ss.encode(),start)

x2 = tdata.find(se.encode(),x1)

请测试并评论输出以确保它能够正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.