0

I am reading EXE (size 2 MB) which have some ASCII text too. Trying to retrieve data as per matched conditions. In Python 2.6, following code is working fine (give me result in 2 sec) but not in 3.2. In Python 3.2 , it keep running forever no response.

Thanks...

match_str = b"sape"
out= ""
try:
    file_obj = open(exe_filePath,'rb')

    while 1:
        data = file_obj.readline(100)

        if data.count(match_str) > 0:               
            out = data.strip()[9:13]                
        if data=="":
            break

    file_obj.close()
    return out
except:
    file_obj.close()
    raise "Some error occurred"
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
aberry
  • 447
  • 3
  • 10
  • 1
    maybe a dumb question - why are you even using a buffered approach? 2MB is hardly a massive file. Just do `f=open(myfile); data=f.read(); do_analysis()`. Less error-prone. – Kurt Spindler Jun 28 '12 at 07:35
  • 1
    Your use of `try/except` is very problematic: First, the scope of the `try` block is way too large - there are too many things that could go wrong. Then, you're using an empty `except` which means you don't get any information about *what* error occurred, so you can't handle it sensibly. At the moment, you're not handling anything anyway (see your `"some error occurred"` message which is a lot less helpful than any traceback Python would have given you if you had simply not used any `try/except` at all). If you only want to make sure your file gets closed, use a `with` block. – Tim Pietzcker Jun 28 '12 at 09:39
  • 1
    There are some other weird things going on. Why are you using `readline()` at all if it's a binary file? Why not `read()`? – Tim Pietzcker Jun 28 '12 at 09:54

1 Answers1

1
if data=="":
    break

will always fail (meaning that the break will never happen) because

>>> b"" == ""
False

and data is a bytes object.

If you change it to

if data==b"":
    break

it should work. Although there are many other things that should be fixed here (see my comment to your question).

I would suggest this:

match_str = b"sape"
with open(exe_filePath,'rb') as file_obj: 
    while True:
        data = file_obj.readline(100)
        if data.count(match_str) > 0:
            # This will overwrite any previous "out" from the last match!
            # Do you really want that?
            out = data.strip()[9:13]
        if data==b"":
            break
return out
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561