读取文件的2个偏移量

时间:2013-09-28 04:12:58

标签: python

我想知道read()函数如何用于读取两个十六进制的偏移量?

我尝试使用它将偏移值转换为int,但是我得到了read。()行的语法错误。有什么想法吗?

OFFSETS = ('3AF7','3ECF')
OFFSETE = ('3B04','3EDE')

for r, d, f in os.walk("."):
    for hahahoho, value in enumerate(OFFSETS and OFFSETE):
        try:
           with open(os.path.join(r,f), 'rb' ) as fileread:
                 texttoprint = fileread.seek(int(OFFSETS[hahahoho], 16) -1)
                 yeeha = texttoprint.read[int(OFFSETS[hahahoho], 16) -1 : int(OFFSETE[damn],16)]
                 print (yeeha)
                 hahahoho + 1

这不是整个代码,只是发布了我需要帮助的那些=(

编辑: 好吧,我想我应该听听你的建议,这是整个代码

nost = 1
OFFSETS = ('3AF7','3ECF')
OFFSETE = ('3B04','3EDE')
endscript = 'No'
nooffile = 1

import os, glob, sys, tempfile

try:
    directory = input('Enter your directory:').replace('\\','\\\\')
    os.chdir(directory)
except FileNotFoundError:
    print ('Directory not found!')
    endscript = 'YES!'

if endscript == 'YES!':
    sys.exit('Error. Be careful of what you do to your computer!')
else:
    if os.path.isfile('Done.txt') == True:
        print ('The folder has already been modified!')
else:
    print ('Searching texts...\r\n')
    print ('Printing...')
    for r, d, f in os.walk("."):
        for HODF in f:
            if HODF.endswith(".hod") or "." not in HODF:
                for damn, value in enumerate(OFFSETS and OFFSETE):
                    try:
                        with open(os.path.join(r,HODF), 'rb' ) as fileread:
                            fileread.seek(int(OFFSETS[damn],16) -1)
                            yeeha = fileread.read(int(OFFSETE[damn], 16) - (int(OFFSETS[damn],16) -1))
                            if b'?\x03\x00\x00\x00\x01\x00\x00\x00Leg2.' not in yeeha and b'?\x03\x00\x00\x00\x01\x00\x00\x00Leg2_r.' not in yeeha:
                                print (yeeha)
                        damn + 1
                    except FileNotFoundError:
                        print('Invalid file path!')
                        os._exit(1)
                    except IndexError:
                        print ('File successfully modified!')
                nooffile = nooffile + 1
                nost = 1

    print ('\r\n'+str(nooffile)+' files read.',)        
    print ('\tANI_list.txt, End.dat, Group.txt, Head.txt, Tail.dat files ignored.')
    print ('\r\nFiles successfully read! Hope you found what you are looking for!')

我可能知道它有什么问题吗?因为它适用于我

1 个答案:

答案 0 :(得分:6)

您的代码还有其他问题,但听起来您想自己解决这个问题。当从文件中读取特定字节范围时,您可以这样做:

start = 1000
end = 1020    # Just examples
fileread.seek(start)
stuff = fileread.read(end - start)

也就是说,首先寻找起始位置,然后根据需要读取多少字节(在本例中为20)。

修改

您的代码唯一真正的“问题”是您以奇怪而奇怪的方式使用enumerate,这使得它完全没必要。表达式OFFSETS and OFFSETE将简单地评估为OFFSETE,使OFFSETS and完全多余。然后,您实际上只使用enumerate(索引)中的第一个值,这使enumerate本身变得多余:您可能只是使用了range(len(OFFSETE))

然而,更恰当的是直接循环遍历值而不是通过索引,如下所示:

for start, end in zip(OFFSETS, OFFSETE):
    # snip
    fileread.seek(int(start, 16) - 1)
    yeeha = fileread.read(int(start, 16) - int(end, 16) - 1)

其他的东西更像是可以消除的轻微uglinesses,使你的代码更好,但严格来说并不是错误的。其中包括你不需要将偏移量表示为字符串,而是可以使用十六进制文字;您无缘无故地多次打开文件,hohohaha + 1表达式完全是多余的,并且您可以直接将- 1额外的偏移量烘焙到您的实际偏移量中,而不是稍后添加它。

我会把它写得更接近这个:

OFFSETS = [0x3AF7 - 1, 0x3ECF - 2]
OFFSETE = [0x3B04 - 1, 0x3EDE - 2]

for r, d, f in os.walk("."):
    for fn in f:
        with open(os.path.join(r, fn), "rb") as fp:
            for start, end in zip(OFFSETS, OFFSETE):
                fp.seek(start)
                yeeha = fp.read(start - end)
                # Do whatever it is you need with yeeha