0

I am trying to parse a txt file with a bunch of MD5 hash passwords (for an assignment) my code is

with open('weak.txt') as f:
    i = 0
    for line in f:
        weak.append(eval(line.strip()))
        if 'str' in line:
            break

but I am a getting

SyntaxError: unexpected EOF,   File "<string>", line 1
    1660fe5c81c4ce64a2611494c439e1ba
                                   ^

I tried to add raw input to my code , but it just hangs. any ideas?

5
  • 4
    Why are you using eval on MD5 codes? eval expects its argument to be a string containing Python statements. Commented Feb 28, 2014 at 1:44
  • because all of the MD5 passwords are strings that I am just trying to get from the txt file. Am I completely off track ? Commented Feb 28, 2014 at 1:45
  • Yes, you are completely off trackline. Commented Feb 28, 2014 at 1:46
  • 2
    yes. try just removing eval and see where that gets you Commented Feb 28, 2014 at 1:46
  • 1
    What you get from the text file are already strings. Commented Feb 28, 2014 at 1:46

1 Answer 1

2

You should not be calling eval.

with open('weak.txt') as f:
    i = 0
    for line in f:
        weak.append(line.strip())
        if 'str' in line:
            break

The eval function tries to interpret the string as a series of python statements. You don't want that . There is generally not a good reason to use eval. You want the md5sum as a string. When you iterate over f with the statement for line in f, you are asking the file object to return string objects to you for each line in the file. In other words, line already contains what you want, you don't need to ask the python interpreter to execute it.

Sign up to request clarification or add additional context in comments.

7 Comments

...especially on any type of input, be it from a file, user input, arguments, or a downloaded web page
@mhlester, true, though that is usually when eval (if ever) would be used: when you don't have the original source and need to run code dynamically. Otherwise, just run it with the rest of your code.
@PaulDraper Your statement makes zero sense. "You don't have the the original source and need to run code dynamically". How would using eval solve the problem of not having the source code?
@EricUrban, I could be more clear. The code must not be in the PYTHONPATH; it's in database or coming from some other input. eval is what you do when you didn't have the source code (in your PYTHONPATH) originally, but you acquired somehow during the course of your program.
@ericurban note eval will execute compiled code objects as well as text-based source code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.