Martin Blume wrote: > "Steve Holden" schrieb >>> [ difference between exec open(fname).read() >>> and for line in open(fname): exec line ] >>> >>> So it seems to depend on the way the file is read. >>> >> It depends on the way the lines of the file are executed, >> not how they are read. >> > Could you elaborate a little bit more on the difference? > I assumed that because read() reads the whole file, the > body of my function sowhat() is present, so that it can > be parsed while the invocation of exec is still running. > If it is read and exec'd line by line, the definition of > the function is still left open at the moment exec() ends, > causing the "EOF" error. Hence my statement, "it depends > on the way the file is read". > I simply meant that the whole source has to be presented to the exec statement and not chunked into lines.
Clearly I could read all the source in with lines = open(cmd_file).readlines() but if you then proceed to try and execute the source line by line as in for l in lines: exec l you will hit problems because of the disjoint nature of the execution which will breal up indented suites and so on. I was probably just a little over-zealous in pursuing correct English usage, in which case please accept my apology. > >> And you may remember the original poster was >> proposing this: >> >> inp = open(cmd_file) >> for line in inp: >> exec line >> >> As for your first example, why not just use execfile() ? >> > I assume that > execfile(fname) > is equivalent to > exec open(fname).read() ? > Pretty much. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC/Ltd http://www.holdenweb.com Skype: holdenweb http://del.icio.us/steve.holden ------------------ Asciimercial --------------------- Get on the web: Blog, lens and tag your way to fame!! holdenweb.blogspot.com squidoo.com/pythonology tagged items: del.icio.us/steve.holden/python All these services currently offer free registration! -------------- Thank You for Reading ---------------- -- http://mail.python.org/mailman/listinfo/python-list