0

I was looking for some help with a program I am writing for personal use. It is supposed to export a movie's name and rating (stored in a dictionary) to an external file (file.txt) and then load it at the start of every run. It should also export a movie's name and review (also a dictionary) to another external file (review.txt). I get an error on the line for reading the file.txt and putting it in base. Any clues?

base = {}
#Open and write info to base
with open('file.txt','r') as f:
    # Error here :(
    base = eval(f.read())

error message:

Traceback (most recent call last):
  File "/Users/Will/Documents/movie_database.py", line 18, in <module>
    base = eval(f.read())
  File "<string>", line 0

    ^
SyntaxError: unexpected EOF while parsing
  • 2
    *"I get an error"* - **what error?!** Please provide the full traceback, and cut the code down to the [minimal example](http://stackoverflow.com/help/mcve) that causes it. – jonrsharpe Nov 01 '14 at 22:45
  • Well why on earth are you just dumping a file into `eval`? That's a huge risk. Look into other methods of persisting Python objects to file (e.g. `pickle`) or recovering them from `repr` form (e.g. `ast.literal_eval`). As it stands, without seeing the content of `file.txt`, it's hard to say exactly what's wrong; is the file empty? – jonrsharpe Nov 01 '14 at 22:49
  • @n00bie: you are passing an empty string to `eval()`. – Martijn Pieters Nov 01 '14 at 22:49
  • What do you mean an empty string? – Will Hodges Nov 01 '14 at 22:50
  • I was using eval because that is how I learned to import information. I am not really interested in using pickle. – Will Hodges Nov 01 '14 at 22:51
  • Yes, the file.txt file is empty. Do I need to add content? I know the format that the default python .write() uses... – Will Hodges Nov 01 '14 at 22:52
  • Well you really should be interested. [`eval` is evil](http://stackoverflow.com/questions/1832940/is-using-eval-in-python-a-bad-practice). – jonrsharpe Nov 01 '14 at 22:52
  • @n00bie Then you're not interested in using python in a more effective and safe fashion. And what does that say to people here about how much effort they should spend helping you? – furkle Nov 01 '14 at 22:52
  • @n00bie: the file is empty, so you get an error. Why are you not interested in using `pickle`? Why the insistence to use the unsuitable option? – Martijn Pieters Nov 01 '14 at 22:53
  • Adding content to file.txt fixed the problem! Thank you @MartijnPeters – Will Hodges Nov 01 '14 at 22:53
  • What is unsafe about using eval and file.write() ? – Will Hodges Nov 01 '14 at 22:54

1 Answers1

2

Your file is empty, so eval() throws an exception because no Python expression was found:

>>> eval('')  # empty string
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 0

    ^
SyntaxError: unexpected EOF while parsing

You shouldn't be using eval() in the first place however, the security implications are terrifying here. Someone that can fool you into loading an arbitrary file can now take over your Python process, and turn your machine into a spam bot zombie.

json or shelve (or directly using pickle, although that has security implications as well) would offer you better and more robust serialisation options. You could also look into using sqlite3 for a SQL database option.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343